0

My requirement is to have a user switch accounts when they click on a specific link. To fully switch accounts, I need to call two cold http observables and then load the data. The order does matter for all three requests.

  1. First switchAccount$
  2. Second updateSession$

Then I just need to get the pageData$ cold observable. I am currently using a resolver, but having difficulties structuring this with rxjs. I only care about the final page data in my component but I just need to make sure the user switches accounts and the session is updated or the backend api call will fail to get the page data.

I have tried to do the following but it is not working:

resolve(route): Observable<any> {
    const switchAccount$ = this.userService.switch('account2').pipe(last());
    const updateSession$ = this.userService.updateSesh();
    const pageData$ = this.pageService.getData();
    const switchAndUpdate$ = updateSession$.pipe(skipUntil(switchAccount$));


    return pageData$.pipe(skipUntil(switchAndUpdate$.pipe(last()); // problem is that switch account and page data get cancelled somehow??

}
2
  • 1
    what's switchDealer$? It's never declared. Commented Jan 3, 2019 at 20:01
  • should be switchAccount$ sorry Commented Jan 3, 2019 at 20:02

2 Answers 2

1

You can combine observable:

resolve(route): Observable<any> {
    const switchAccount$ = this.userService.switch('account2'); // I think last is not helpful
    const updateSession$ = this.userService.updateSesh();
    const pageData$ = this.pageService.getData();

    return switchAccount$.pipe(
         switchMap(() => updateSession$), // Or I think with margeMap works too
         switchMap(() => pageData$),
         catchError(error => of(error))
    )
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use switchMap for this:

return obs1$.pipe(
    switchMap(() => obs2$),
    switchMap(() => obs3$),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.