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

前端 未结 7 1935
囚心锁ツ
囚心锁ツ 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 03:58

    It worked for me like this. I don't know if it is a right approach but could help to reduce lines

    function myFun(){
         var deffer = $q.defer();
         angular.forEach(array,function(a,i) { 
              Service.method(a.id).then(function(res) { 
                   console.log(res); 
                   if(i == array.length-1) { 
                          deffer.resolve(res); 
                   } 
              }); 
         });
         return deffer.promise;
    }
    
    myFun().then(function(res){
         //res here
    });
    

提交回复
热议问题