在 Angular 6 中按顺序执行 http 请求 [英] Execute http request sequentially in Angular 6
本文介绍了在 Angular 6 中按顺序执行 http 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要依次向服务器发送多个HTTP PUT请求(在完成上一个请求后才开始下一个请求,请求数量不固定).如果我使用以下源代码,将发送所有请求
I need to send multiple HTTP PUT request to server sequentially (after the next request is only started after completing the previous request, the number of request is not fixed). If I use the below source code, all request will be sent
`listURL.forEach(url => {
const req = new HttpRequest('PUT', url, formData, { reportProgress: true});
httpClient.request(req).subscribe(event=>{});
});`
有没有办法按顺序执行请求?
Is there anyway to execute the requests sequentially?
推荐答案
如果你不熟悉 rxjs,或许你可以试试下面的方法?
If you are not familiar with rxjs, perhaps you can try the following?
let requests = listURL.map(url => new HttpRequest('PUT', url, formData, { reportProgress: true});
function do_seq(i) {
httpClient.request(requests[i]).subscribe(event => {
if (i < requests.length - 1) do_seq(i + 1);
});
}
do_seq(0);
这篇关于在 Angular 6 中按顺序执行 http 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文