在Angular 6中顺序执行http请求 [英] Execute http request sequentially in Angular 6

查看:713
本文介绍了在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆