balteo
balteo

Reputation: 24659

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 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?

Upvotes: 1

Views: 136

Answers (1)

martin
martin

Reputation: 96891

You can use map and just ignore the second result:

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

Upvotes: 4

Related Questions