Call multiple API using rxjs and return it as obserable

Viewed 70

I am having 4 API let be A,B,C,D and I want to get combine data of all 4 API into single obserable.

I want using forkJoin([A,B]) and using the response of forkJoin into forkJoin([C,D]). Example:

return forJoin([a,b]).pipe(map result=>{
 return forkJoin([c(a.result),d(a.result,b.result)]);
})

Problem for is that I am only getting C and D API response. Anyone can help me out.

1 Answers

First of all there should be a switchMap for returning the inner forkJoin. And if you want to return also the a and b response, you can just use of the original result:

return forkJoin([a,b]).pipe(
  switchMap(result => {
    return forkJoin([c(result[0]),d(result[0], result[1]), of(result)]
  })
})

Now you get an array of all 3 results, last beeing the array of the first response

Related