Chain observables in sequence and return value from last call

1.7k Views Asked by At

I have a route resolver where I need to make two method calls in sequence that both return observables. I do not need to do anything with the response from the first call, but I would like to return to the component for the route the data that comes back in the second call. Can someone give me an example of how to do this? I think I should be using flatMap() but I cant seem to get it to work.

So basically: firstMethod(): Observable -> secondMethod():Observable <--return value from this.

1

There are 1 best solutions below

2
matmo On

You basically want something like this:

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<string> {
      return this.someService.firstCall()
         .switchMap(res => this.someService.secondCall())
   }

You can also map the secondCall() to a more specific value if necessary.