简体   繁体   English

在下一个请求之前等待$ http promise

[英]Wait for $http promise before the next request

I'm working on an angularJS app and this is my first website using this framework. 我正在开发一个angularJS应用程序,这是我使用这个框架的第一个网站。 In my app i've a need to make a $http call inside a for loop. 在我的应用程序中,我需要在for循环中进行$ http调用。 With in the loop before the next iteration I want to wait for the response from my previous call. 在下一次迭代之前的循环中,我想等待前一次调用的响应。 What is the best and simple way to do this. 什么是最好和最简单的方法来做到这一点。 I've tried using the callBack, $q.all(), .then in all of these only the last request is going through. 我已经尝试过使用callBack,$ q.all(),. then在所有这些中只有最后一个请求正在通过。 Please help. 请帮忙。

Note: My API that i'm calling through $http can't queue requests. 注意:我通过$ http调用的API无法对请求进行排队。

Edit: I've tried both the below approaches, in both of the cases only the last request is being made successfully. 编辑:我已经尝试了以下两种方法,在这两种情况下,只有最后一个请求成功。 Can you tell me what is wrong i'm doing here. 你能告诉我我在这里做错了吗?

Approach 1: 方法1:

var promiseArray=[];

for(var i=0;i<items.length;i++)
{
 var promise=services.Post(items[i]);
 promiseArray.push(promise);
}

$q.all(promiseArray).then(data)
{
 ...
}

Approach 2: 方法2:

var promises = [];

for (var i = 0; i < items.length; i++) {

    var deffered = $q.defer();
    var promise = services.Post(items[i]);
    promise.success(function(data) {
        deffered.resolve(data);
    })
    promises.push(deffered);
}

var result = $q.all(promises); var result = $ q.all(promises);

EDIT :2 Service Code: 编辑:2服务代码:

Services.Post = function(lineItemId, submitUrl) {
    var url = (submitUrl) ? submitUrl : Services.serviceUrl;

    return $http.post(url, {
        "LineItemID": lineItemId
    }).
    success(function(data, status, headers, config) {
        Services.processResponse(data, status, headers, config);
    }).
    error(function(data, status, headers, config) {

        JL('Angular').error('Error response when calling Service ' + config);

    });
};

You could use $q.when which would take promise of $http & when it gets resolved it call the function inside .then 你可以使用$q.when ,它会接受$http承诺,当它被解析时,它会调用里面的函数.then

$q.when(promiseObj).then(callback);

If there are multiple promise then you could use $q.all which would accept array of promise & called then function when all promise inside the function gets resolved. 如果有多个promise,那么你可以使用$q.all ,当函数内的所有promise都被解析时,它会接受promise的数组并调用then函数。

$q.all([promiseObj1, promiseObj2]).then(callback);

Update 更新

I think your 1st approach is right only missed couple thing 我认为你的第一种方法是对的,只是错过了几件事

  1. for loop condition, which does create an extra object which would be undefined for循环条件,它确实创建了一个未定义的额外对象
  2. $q.all should have function inside .then $q.all应该有内部的功能.then

Code

var promiseArray=[];

for(var i=0; i < items.length - 1;i++) //<-- change from i<items.length
{
 var promise=services.Post(items[i]);
 promiseArray.push(promise);
}

$q.all(promiseArray).then(function(data) //<-- it should be function in .then
{
  //this will called after all promises get resolved.
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM