Skip to content

Instantly share code, notes, and snippets.

@zuhairkareem
Last active March 3, 2020 07:38
Show Gist options
  • Select an option

  • Save zuhairkareem/04472d0017667e27c6cf5f569e36403c to your computer and use it in GitHub Desktop.

Select an option

Save zuhairkareem/04472d0017667e27c6cf5f569e36403c to your computer and use it in GitHub Desktop.
Sequential HTTP calls using RxJS in Angular 8

Make sequential HTTP requests using RxJS in Angular 8

http1$, http2$, http3$, http4$ are API requests made using angular HTTP client library

this.apiService
      .http1$
      .pipe(
        concatMap((http1Result: any) => {
          // I have status property returned in response if there is any error,
          // use catchError operator if 500 or other errors are returned in your APIs
          if (!http1Result.status) {
            // Stop the observable chain.
            return EMPTY;
          }
          // If no error, call next API.
          return this.apiService.http2$;
        }),
        concatMap((http2Res: any) => {
          if (!http2Res.status) {
            return EMPTY;
          }
          return this.apiService.http3$;
        }),
        concatMap((http3Result: any) => {
          if (!http3Result.status) {
            // Do something
          }
          return this.apiService.http4$;
        })
      )
      .subscribe((result: any) => {
        console.log('Final Subscribe: ');
        console.log(result);
        // Do some thing you want.
      });

Ref: Good resource on different operators https://blog.angular-university.io/rxjs-higher-order-mapping/

Error handling https://blog.angular-university.io/rxjs-error-handling/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment