0

Suppose I have an array var numbers = [0, 1, 2] and an $http request.
I want to chain as many .then() functions after the $http.get() as there are items in numbers. So I want code like

//for each item in numbers
.then(function(){
  $http.get(...)
   .then(function(){
     $http.get(...)
      .then(...)
   })
})

so that the code runs as

.then(
   $http.get(...)
     .then(
        $http.get(...)
          .then(
             $http.get(...)
          )
     )
)

Is that possible?

Clawish
  • 2,934
  • 3
  • 24
  • 28

1 Answers1

3
$http.get().then(function() {
    numbers.forEach(callback);
});

Couldn't you just loop thru them? (hint: the answer is yes)

With your edit, you need a different loop! Weee…

var i = 0; //Mind the scope (thanks @PaulS.) - a function wrapper is a good idea
function callback() {
    if (i++ < numbers.length) $http.get().then(callback);
};
$http.get().then(callback);
bjb568
  • 11,089
  • 11
  • 50
  • 71