0

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.

Maciej Treder
  • 11,866
  • 5
  • 51
  • 74
user1373121
  • 999
  • 4
  • 13
  • 22
  • 1
    Code example would be very helpful. At the moment it's hard to figure out why you are making two calls. – Huske Jun 19 '17 at 23:39

1 Answers1

1

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.

matmo
  • 1,339
  • 12
  • 17
  • why switchmap here? – Jota.Toledo Jun 20 '17 at 07:17
  • You could use switchMap or flatMap here. If the stream emits another event, when the event got to the switchMap, it would cancel the previous network request if there was one, whereas flatMap would let the previous request continue. Because the `resolve` function is only going to have one item in it's event stream/sequence, it shouldn't make a difference if you use switchMap or flatMap here - I just chose it out of habit. – matmo Jun 20 '17 at 16:44