我想并行进行两个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));我怎么能这样做?
=> https://stackblitz.com/edit/playground-rxjs-263xwk这里的Stackblitz游乐场
发布于 2022-02-19 07:59:35
您可以对并行请求使用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)
})https://stackoverflow.com/questions/71183027
复制相似问题