Angular 中连续 HTTP 请求的动态数量 [英] Dynamic amount of sequential HTTP requests in Angular
问题描述
这是我在这里的第一个问题,如果不够详细,请见谅.
我希望按顺序发出动态数量的请求(可能很多)以获取数据,
我需要从每个请求中收集这些数据,并在最后一个请求结束时作为最终的 observable 返回.
我尝试使用 forkJoin
来组合请求,虽然,这并没有按顺序创建每个请求,然后还有 concat
,它在每个请求之后发出和观察.
您想要:
- 发出任意数量的连续 HTTP 请求(我假设基于某个数组)
- 返回结果数组
我会在这里将 concat
与 toArray
结合使用.concat
将按顺序运行请求,当所有响应都可用时 toArray
将发出一个数组.
//一些动态数组常量源 = [ 1, 2, 3, 4, 5 ];const observables = source.map(x => this.http.get('some url'));连接(...可观察的).管道(toArray()). 订阅(响应 => {控制台日志(响应);//结果数组});
演示:https://stackblitz.com/edit/angular-s1fdxj>
This is my first question here, sorry if it isn't detailed enough.
I am looking to make a dynamic amount of requests sequentially (could be a lot) to get data,
I would need this data to be collected from each request and returned as a final observable at the end of the last request.
I have attempted to use forkJoin
to combine the requests, although, this does not make each request sequentially and then also concat
, which emits and observable after each request.
You want to:
- Make an arbitrary number of sequential HTTP requests (based on some array, I assume)
- Return an array of results
I would use concat
in conjunction with toArray
here. concat
will run the requests sequentially, and toArray
will emit an array when all responses are available.
// some dynamic array
const source = [ 1, 2, 3, 4, 5 ];
const observables = source.map(x => this.http.get('some url'));
concat(
...observables
).pipe(
toArray()
).subscribe(responses => {
console.log(responses);
// array of results
});
DEMO: https://stackblitz.com/edit/angular-s1fdxj
这篇关于Angular 中连续 HTTP 请求的动态数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!