Make angular.forEach wait for promise after going to next object

前端 未结 7 1923
囚心锁ツ
囚心锁ツ 2020-12-03 03:25

I have a list of objects. The objects are passed to a deferred function. I want to call the function with the next object only after the previous call is resolved. Is there

7条回答
  •  独厮守ぢ
    2020-12-03 04:02

    It might help someone as I tried several of above solution before coming up with my own that actually worked for me (the other ones didn't)

      var sequence;
      objects.forEach(function(item) {
         if(sequence === undefined){
              sequence = doSomethingThatReturnsAPromise(item)
              }else{
              sequence = sequence.then(function(){
                   return doSomethingThatReturnsAPromise(item)
                         }); 
                     }
            });
    

提交回复
热议问题