简体   繁体   English

Angular-For循环HTTP回调/承诺

[英]Angular - For Loop HTTP Callback/Promise

I am trying to write a loop which performs a number of http requests and adds each response to a list. 我正在尝试编写一个执行许多http请求并将每个响应添加到列表的循环。

However, I don't think I am going about it quite the right way. 但是,我认为我不会以正确的方式进行操作。

I think I am not implementing the required promises correctly. 我认为我没有正确执行所需的承诺。 The console log after the for loop shows myList array as empty. for循环后的控制台日志显示myList数组为空。

Code: 码:

var _myList = []

function getStuff() {

    var deferred = $q.defer()

    var url = someUrl

    $http.get(url).success(function(response) {

        if ( response.array.length > 0 ) {

            // loop starts here
            for ( var i=0; i < response.array.length; i++ ) {

                getThing(response.array[i].id);

            };
            // check the varibale here

            console.log(_myList);

            deferred.resolve('Finished');

        } else {

            deferred.resolve('No stuff exists');

        };

    }).error(function(error) {

        deferred.reject(error);

    });

    return deferred.promise;

};

function getThing(thindId) {

    var deferred = $q.defer()

    var url = someUrl + thingId;

      $http.get(url).success(function(response) {

            _myList.push(response);

            deferred.resolve(response);

      }).error(function(error) {

            deferred.reject(error);

      });       

    return deferred.promise;

};

You indeed won't be able to populate _myList array with for-loop like you set up. 的确,您将无法像设置一样使用for循环填充_myList数组。 Instead create an array of promises - one per data item in response.array and return it as inner promise. 而是创建一个承诺数组-在response.array每个数据项一个,并将其作为内部承诺返回。

function getStuff() {

    var url = someUrl;

    return $http.get(url).then(function(response) {
        if (response.data.array.length > 0) {
            return $q.all(response.data.array.map(function(data) {
                return getThing(data.id);
            }));
        } else {
            return 'No stuff exists';
        }
    });
}

function getThing(thindId) {
    var url = someUrl + thingId;
    return $http.get(url).then(function(response) {
        return response.data;
    });
}

After that you would use getStuff like this: 之后,您将使用getStuff像这样:

getStuff().then(function(myList) {
    console.log(myList);
});

You can simplify your code as follows: 您可以按如下方式简化代码:

var allThings = response.array.map(function(id){
  var singleThingPromise = getThing(id);
  //return a single request promise
  return singleThingPromise.then(function(){
    //a getThing just ended inspect list
    console.log(_myList);
  })
});
$q.all(allThings).then(function(){
  //only resolve when all things where resolved
  deferred.resolve('Finished');
}, function(e){
  deferred.reject('Something went wrong ' + e);
});

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

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