how to make multiple http calls synchronously at different levels in angular 2 using rxJS

Arunsai B K

I want to make multiple http calls synchronously at different levels in angular 2 using rxJS For Eg , I am having a resolver service which handles 5 api calls like a,b,c,d and e.

I want them to execute in this order and I am not using any of those output values as input so I need all responses only at end.

first a should be called and once api is completed second b,c, d third after b,c,d completed then e should be called

In activated Route , I need to retrieve those values and use it I have already tried below stuff but I am getting response of A and E only and I could not able to fetch the values from inner observable

resolve(a,b){
 return A.flatMap(data=> {
      return forkJoin (
                 B,C,D)
                  }).flatMap(xres => {
                                 return E
                                 })
}
route.data.dataA
route.data.dataBCD
route.data.dataE
Jota.Toledo

If you have one resolver that takes care of all 5 requests, in order to ensure their order of execution, then it would make sense to return the results of those 5 requests as one element. Try with the following:

import {Observable, forkJoin} from 'rxjs';
import {mergeMap, map} from 'rxjs/operators';

interface AFoo { }
interface BFoo { }
interface CFoo { }
interface DFoo { }
interface EFoo { }

class FooService {
  doA(): Observable<AFoo> { throw new Error("Not implemented"); }
  doB(): Observable<BFoo> { throw new Error("Not implemented"); }
  doC(): Observable<CFoo> { throw new Error("Not implemented"); }
  doD(): Observable<DFoo> { throw new Error("Not implemented"); }
  doE(): Observable<EFoo> { throw new Error("Not implemented"); }
}

interface CompositeFoo {
  aFoo: AFoo;
  bFoo: BFoo;
  cFoo: CFoo;
  dFoo: DFoo;
  eFoo: EFoo;
}

export class FooResolver implements Resolve<CompositeFoo>{
  constructor(private fooService: FooService) { }
  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<CompositeFoo> {
    const forked$ = forkJoin(this.fooService.doB(), this.fooService.doC(), this.fooService.doD())
                    .pipe(map(([bFoo, cFoo, dFoo]) => ({ bFoo, cFoo, dFoo })));
    return this.fooService.doA()
      .pipe(
      mergeMap(aFoo => forked$.pipe(map(forked => ({ ...forked, aFoo })))),
      mergeMap(result => this.fooService.doE().pipe(map(eFoo => ({ eFoo, ...result })))),
      map(result => ({ ...result }))
      );
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

make multiple http calls synchronously in AngularJs

Angular 8 RXJS - Make multiple HTTP calls sequentially

Angular - Correctly using RXJS expand operator to make recursive http calls

How to wait for multiple HTTP calls to be completed in Angular with RXJS?

How to make synchronous http calls in angular 2

How to make recursive HTTP calls using RxJs operators?

How do I parallelise multiple HTTP calls using RxJS

Angular/Rxjs run store (ngrx) calls synchronously

How to make multiple HTTP method calls in AWS using HTTP API?

Multiple setState() calls in a React method: How to make it work "synchronously"

How to make a sequence of http requests in Angular 6 using RxJS

Angular - Make multiple HTTP calls sequentially

Angular2 & RxJS multiple HTTP calls - display as soon as first response returned

Synchronously process Observable to prevent too many HTTP requests from timing out using rxjs Angular and ngrx

Angular 2 combine three http calls with flatMap? RxJs?

Multiple sequential API calls using RXJS in Angular 12 app

How can make multiple factor columns with different levels ordered?

Create JSON Object multiple calls Angular 2 RXJS

How to chain multiple .map() calls on previous items using rxjs

How to call multiple dependent api calls with time intervals using RxJS

How to chain Http calls in Angular2?

How to control the flow of service calls, using rxjs with Angular 9?

What is the way in RxJS to keep collecting results from multiple and different http calls independently on when one of those fails?

How to make a put request in angular2 using http?

Angular rxjs sets of sequential http calls

Angular - Stream multiple HTTP calls 2 per 2

Merge different HTTP calls which are dependent on each other in RxJS/Angular 6

How can I make multiple nested http request in RxJs?

How to make infinite calls for a div reload through Ajax synchronously