Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJS switchMap and passing response to another operator

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?

like image 869
Adam Adamski Avatar asked Sep 19 '25 11:09

Adam Adamski


1 Answers

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');
        }
    });
like image 99
ZahiC Avatar answered Sep 22 '25 08:09

ZahiC