3

Let say I have code like that:

            this.apiService.getUrlForPhoto('photo1')
            .switchMap((url) => {
                return this.apiService.uploadPhotoToProvidedUrl(url);
            })
            .switchMap((response) => {
                return this.apiService.confirmPhotoUpload(confirmationToken);
            })
            .subscribe((response) => {
                if (response.confirmed) {
                    console.log('Success');
                }
            });

First is http get which returns url where I can post new photo, Second is http put where I have to upload photo, Third is http post where I have to confirm that photo was uploaded to that url.

My question is it is possible to pass url to second switchMap? At second switchMap I have response from uploadPhotoToProviderdUrl method but how can I get response from previous method?

Adam Adamski
  • 737
  • 3
  • 11
  • 20

1 Answers1

5

Assuming all your API calls are Observables:

this.apiService.getUrlForPhoto('photo1')
    .switchMap((url) => {
        return this.apiService.uploadPhotoToProvidedUrl(url)
            .switchMap((response) => {
                // Here you can use 'url'
                return this.apiService.confirmPhotoUpload(confirmationToken);
            })  
    })
    .subscribe((response) => {
        if (response.confirmed) {
            console.log('Success');
        }
    });
ZahiC
  • 13,567
  • 3
  • 25
  • 27