![](/img/trans.png)
[英]How to make multiple HTTP requests with RxJS and merge the response into one payload and map it to another action?
[英]rxjs merge http response
多个http请求是并发的,并发请求数可以控制。 完成所有请求后,可以合并数据(数据可以是有序的,也可以是无序的)
from([link, link, link]).pipe(
merge(l => request.get(l)),
// something...
)
return [linkdata, linkdata, linkdata]
考虑实现它,如下所示:
from([link1, link2, link3]).pipe(
mergeMap(request.get, CONCURRENT_NUMBER), // <= limit the number of active inner subscriptions at a time
toArray() // <= transform stream into array prior to completion
)
.subscribe(([result1, result2, result3]) => { })
好像你需要使用forkJoin
const links = [link1, link2, link3]; // assump `links` is an array of string
forkJoin(
from(links).pipe(
mergeMap((link: string) => {
return http.get(link); // returns an observable
}),
toArray()
)
).pipe(
map(array => array[0]) // data stream is in [[data]] format, you only need head
); // [link1data, link2data, link3data]
我想你正在寻找mergeAll
from([link1, link2, link3].map(link => request.get(link))).pipe(
mergeAll(CONCURRENT_NUMBER),
// something like reduce, toArray... to combine the results
).subscribe(console.log);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.