Skip to content Skip to sidebar Skip to footer

Angularjs Foreach Loop Through Http Requests Dependent On Previous Request

I want to loop through an array to perform $http.jsonp requests in angular, however each request will be slightly different depending on the timestamp of the previous $http.jsonp r

Solution 1:

You need to continue to chain each subsequent request while looping over the array/hash.

Conceptually it would look like this:

function callServiceForEachItem() {
  var promise;

  angular.forEach(items, function(item) {
    if (!promise) {
      //First time through so just call the service
      promise = fakeService.doSomething(item);
    } else {
      //Chain each subsequent request
      promise = promise.then(function() {

        return fakeService.doSomething(item);
      });
    }
  });
}

And just to get a better understanding, here is a Plunker that shows how to perform this chaining.


Post a Comment for "Angularjs Foreach Loop Through Http Requests Dependent On Previous Request"