How to manage multiple sequential subscribe methods in Angular and RxJs?

Shofol

I need to call two http service calls and subscribe them after subscribing another one http service call. I mean, the operation will be sequential as the last two calls depend on the first one. Can this operation be handled with concatMap of RxJs?

I solved the problem using nested subscription. But, I think using concatMap, this can be done. In all examples I searched, there are two calls which are concatenated. How to concat more than two subscription and solve the issue.

this.accountService.fetchBranchCodeLength().subscribe(
        data => {
            this.branchCodeLength = +data;


    //Now, I need to call another service to calculate 
    accountNumberLengthWithProductCode//


   this.subscribers.fetchAcoountNumberLength = 
    this.accountService.fetchAccountLengthConfiguration().subscribe(
      accountLength => {
        this.accountNumberLengthWithProductCode = 
            (+accountLength) - (+this.branchCodeLength);});


    //Another same kind of call to calculate 
    SUBGLCodeLength//


   this.subscribers.fetchSUBGLCodeLengthSub = 
     this.glAccountService.fetchSUBGlCodeLength(SUBGlCodeQueryParam)
      .subscribe(length => this.SUBGLCodeLength = 
          (+length.value) - (+this.branchCodeLength)                        
    );
  }
);
Amir Arbabian

Concat map is used when you have a lot of requests, but when they are similar, it means they will have the same output data and handler, which is not your case. So you can use forkJoin, because there are no any dependencies among your calls.

Code

forkJoin(
   this.accountService.fetchBranchCodeLength(),
   this.accountService.fetchAccountLengthConfiguration(),
   this.glAccountService.fetchSUBGlCodeLength(SUBGlCodeQueryParam)
).subscribe(([data, accountLength, length]) => {
   this.branchCodeLength = +data;
   this.accountNumberLengthWithProductCode = (+accountLength) - this.branchCodeLength
   this.SUBGLCodeLength = (+length.value) - this.branchCodeLength;
});

And by the way, you do not need to unsubscribe from http call observables, because they are finite (to be precise the emit only one event).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Difference between the methods .pipe() and .subscribe() on a RXJS observable

angular2 / RxJS - how to retry from inside subscribe()

Angular 5 Rxjs Subject.subscribe() not triggering in multiple components

Angular resolve Rxjs map and subscribe multiple APIs

How to return value from subscribe of forkJoin with angular 6 and RxJs 6?

RxJs Angular 7 HttpClient multiple POST with forkJoin remove second subscribe?

Angular rxjs sets of sequential http calls

Angular RxJS Nested Subscribe with multiple Http Requests

How to refactor a subscribe inside a subscribe in rxjs

Angular HttpInterceptor: How to use RxJS for multiple conditions

How to call multiple API and subscribe in angular 6?

Angular RxJS best practices to subscribe multiple http requests depending from previous result

How to chain API calls in rxJs subscribe and map with Angular?

How to manage multiple observable in angular 7

How to use subscribe-next RxJS in Angular?

How to manage multiple local methods in java rmi coding

Angular typeahead with RxJs switchMap subscribe

Angular | Subscribe to multiple observables

How to subscribe multiple observable in rxjs 6 ? do is not a function in rxjs 6

Multiple Nested .subscribe in Angular

How to subscribe to date chage with Rxjs

How to call the API in sequential order in Angular9 with rxjs?

angular RXJS how to cast subscribe response type

Multiple sequential API calls using RXJS in Angular 12 app

Angular RxJS sequential requests issue

subscribe keys of and observable in Angular/RxJS

How can I subscribe or merge new Observable in RXJS and Angular

How to reactively subscribe to URL change and get URL in Angular RxJS?

How do I replace subscrib in a subscribe in Angular RxJS?