RxJs:获取可观察数组
RxJs: get array of observables
我有以下伪代码
zip(
...[1, 2, 3].map((id) =>
this.http.function1(id).pipe(
mergeMap((obj1) => this.http.function2(obj1.id)),
mergeMap((obj2) => this.http.function3(obj2.id)),
),
),
).subscribe((result) => {
console.log('This should be an array of all the results of the requests to this.http.function3');
});
我想要所有请求的结果。我该怎么做?
而不是 zip()
,我会使用 from()
发出数组的每个值,然后在管道的末尾,您可以应用 toArray()
将所有发出的值组合到所有 HTTP 请求完成后的数组。
from([1, 2, 3]).pipe(
mergeMap(id =>
this.http.function1(id).pipe(
mergeMap(obj1 => this.http.function2(obj1.id)),
mergeMap(obj2 => this.http.function3(obj2.id))
)
),
toArray()
).subscribe(result=>{
console.log('Result should now be an array from function3', result);
});
我有以下伪代码
zip(
...[1, 2, 3].map((id) =>
this.http.function1(id).pipe(
mergeMap((obj1) => this.http.function2(obj1.id)),
mergeMap((obj2) => this.http.function3(obj2.id)),
),
),
).subscribe((result) => {
console.log('This should be an array of all the results of the requests to this.http.function3');
});
我想要所有请求的结果。我该怎么做?
而不是 zip()
,我会使用 from()
发出数组的每个值,然后在管道的末尾,您可以应用 toArray()
将所有发出的值组合到所有 HTTP 请求完成后的数组。
from([1, 2, 3]).pipe(
mergeMap(id =>
this.http.function1(id).pipe(
mergeMap(obj1 => this.http.function2(obj1.id)),
mergeMap(obj2 => this.http.function3(obj2.id))
)
),
toArray()
).subscribe(result=>{
console.log('Result should now be an array from function3', result);
});