RXJS: Return the manipulated value of outer observable

Anjil Dhamala

I am trying to call two observables, with the response from second I manipulate the first and then return a new observable.

return this.http.get(requestUrl)
            .map((response: User) => {
                sessionManager.currentUser = response;
                return this.isFirstTimeUser(response.id);
            })
            .do((response: any) => {
                console.log(response);
                sessionManager.currentUser.firstTimeLogin = response === 'true';
                return Observable.create(sessionManager.currentUser);
            })
            .toPromise();

isFirstTimeUser(response.id) is another function that returns an observable.

private isFirstTimeUser(userId: number): Observable<any> {
        const requestUrl = 'someUrl';
        return this.http.get(requestUrl, {responseType: 'text'});
    }

All I want to do is get the user object from the first http call, make another request using the user's id in the second call and then after I get back the response from the second observable, I update a property of the first response and return the updated first response. How do I achieve this? What rxjs function should I be looking at?

Reactgular

You were close in your example.

You want to use switchMap which unsubscribes to the incoming observable, and subscribes to the next observable.

return this.http.get(requestUrl)
        .switchMap((response: User) => {
            sessionManager.currentUser = response;
            return this.isFirstTimeUser(response.id);
        })
        .map((response: any) => {
            console.log(response);
            sessionManager.currentUser.firstTimeLogin = response === 'true';
            return sessionManager.currentUser;
        })
        .toPromise();

The response will be passed to isFirstTimeuser() and the following .map() will be applied to the observabled returned by that function.

It's called switchMap because the observable returned by this.http.get() is unsubscribed and the next observable is used. This won't be a problem with this.http.get(), but keep in mind that switchMap only works for the first emitted value.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Rxjs: Wait for outer observable, pass value to inner and return both resolved values in subscription

Return Rxjs Observable that receives a value after some condition is fulfilled

filter a single value with RxJS in and return an observable and use the observable via async in a template in Angular

rxjs expand, outer observable executing before inner observable finishes

Angular - RxJS - Observable and method return

RxJs return Observable if variable not undefined

Return a Observable from a Subscription with RxJS

RxJS/Observable flatMap can return Observable or array

RxJs get value from observable

Rxjs combinelatest observable value is undefined

Rxjs: shorthand of create observable from value or observable

RxJS - Return subject with value

How to chain rxjs observables to get value of inner observable and return with parent in Angular

Better way to return conditional rxjs Observable

Rxjs Observable run sequentially and return an array

How to return stubbed Observable<void> in RxJS

RxJS Sequential Request and return Observable<> first response

Modify array in observable with rxjs, return whole object

RXJS - Angular : Two Observable<booleans> should return Observable<boolean>

how to wait for an observable but return the previous observable result in rxjs?

RxJS: How to subscribe to inner observable only when outer observable/subject is subscribed to?

Rxjs assign value in observable to global variable

How to skip first value of an observable with RxJS?

How to get current value of RxJS Subject or Observable?

RxJS - Observable is not emitting value with .next() on first call

RxJs Observable - Handle 404 by emitting default value

Rxjs: Execute code and emit value on completion of Observable

Problem returning value from RxJS Observable

RxJS Error Observable when another emits value

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive