按顺序/系列发出http请求 [英] Make http requests in order / series

查看:221
本文介绍了按顺序/系列发出http请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用作业构建了一个数组.作业按开始和结束事务分组.交易是对Web服务的调用.整个交易看起来像这样:

I built an array with jobs. Jobs are grouped by a start and end transaction. A transaction is a call to the webservice. An entire transaction can look like this:

  1. 创建交易(调用/opentransaction)
  2. DoSomething A(呼叫/a)
  3. DoSomething B(呼叫/b)
  4. 等.
  5. 关闭交易(调用/closetransaction)

我的伪代码看起来像这样:

My pseudo code looks something like this:

for (var i=0;i<jobs;i++)
{
  $http(job);
}

这很好用,但是顺序是随机的.我需要确保订单保持不变.如何执行?

This works fine, but the order is random. I need to make sure that the order is maintained. How can enforce that?

推荐答案

保存您的$http调用的承诺,让下一个作业等待,直到上一个作业完成.这可能看起来像这样:

Store away the promise of your $http call and let the next job wait until the previous is done. This could look something like this:

var promise = $q.when(true);

for (var i = 0; i < jobs; i++) {
  promise = promise.then(function () {
    return $http(job);
  })
}

这篇关于按顺序/系列发出http请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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