Search code examples
angularrxjsangular-httpclientrxjs6

Using RXJS to issue two sequential http calls and returning the result of the first one only


I am using RXJS 6 together with Angular 6 HttpClient and I am trying to execute two http calls sequentially.

  • I need to return the result of the first call only.
  • The first call must be issued before the second.

I thought of using the tap method as follows:

someMethod(items: Item[]): Observable<Item[]> {
   const one = this.http.put<{ items: Item[] }>('urlOne', items);
   const two = this.http.put<void>('urlTwo', items);

   return one.pipe(
     mergeMap((res)=> res.items),
     tap(two)
   );
}

Is there a better way? If so how?


Solution

  • You can use map and just ignore the second result:

    one.pipe(
      mergeMap((res) => two.pipe(
        mapTo(res)
      ),
    );