我如何在 RXJS 中并行调用 2 API 和紧随其后的第三个?

How do I call 2 API in parallel and the third right after that in RXJS?

我想并行进行 2 个 API 调用,然后立即进行第三个调用。 我能够 运行 API 使用合并映射并连续使用 concatMap 并行调用。我想把两者结合起来。

//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));

我该怎么做?

这里是 Stackblitz 游乐场 => https://stackblitz.com/edit/playground-rxjs-263xwk

您可以使用 forkJoin 进行并行请求。

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)
})