I want to make 2 API calls in Parallel and then the third immediately after that. I am able to run API calls in Parallel using merge map and consecutively using concatMap. I want to combine the two.
//Call API1 and API2 in Parallel
// from([apiCall(1), apiCall(2)]).pipe(
// mergeMap(e=>e)
// ).subscribe(x => console.log(x));
//Call API1 and API2 consecutively
// from([apiCall(1), apiCall(2)]).pipe(
// concatMap(e=>e)
// ).subscribe(x => console.log(x));
//Call API1 and API2 in Parallel and API 3 right after both finishes
from([apiCall(1), apiCall(2), apiCall(3)]).pipe(
// ????
).subscribe(x => console.log(x));
How can I do such?
Stackblitz playground here => https://stackblitz.com/edit/playground-rxjs-263xwk
CodePudding user response:
You can use forkJoin
for parallel requests.
forkJoin([apiCall(1), apiCall(2)]).pipe(
concatMap((response) => apiCall(3).pipe(
map((res) => [...response, res])
))
).subscribe((response) => {
response[0]; // result from apiCall(1)
response[1]; // result from apiCall(2)
response[2]; // result from apiCall(3)
})