RS17
RS17

Reputation: 813

How to use the response of one api call in forkJoin as a parameter to another api call?

I want to make c call with the response of b as the parameter. How to implement this?

resolve(
            route: ActivatedRouteSnapshot,
            state: RouterStateSnapshot
        ): Observable<any> {

            let a = this.loginService.getApplicationCentre();
            let b = this.loginService.getLoginId();
            let c = this.loginService.getUserDetails(response of (b))

            let join = forkJoin(a, b, c).pipe(map((allResponses) => {
                return {
                    A: allResponses[0],
                    B: allResponses[1],
                    C: allResponses[2]
                };
            }));

            return join;

        }

Any help would be appreciated!

Upvotes: 3

Views: 651

Answers (1)

Jota.Toledo
Jota.Toledo

Reputation: 28434

This can be done as follows:

let a$ = this.loginService.getApplicationCentre();
let bc$ = this.loginService.getLoginId()
  .pipe(
    mergeMap(b => this.loginService.getUserDetails(b)
      .pipe(map(c => [b, c])))
  );
return forkJoin(a$, bc$).pipe(map(([A, [B, C]]) => ({ A, B, C })));

Upvotes: 3

Related Questions