Javascript 循环等待,直到在下一次迭代之前解决所有承诺

Javascript 循环等待,直到在下一次迭代之前解决所有承诺,javascript,angularjs,promise,angular-promise,Javascript,Angularjs,Promise,Angular Promise,这是我对AngularJS的要求: for (var i = 0, len = self.Scope.data.length; i < len; i++) { var data = self.Scope.data[i]; var self = this; //First asynchronous function self.EcritureService.createNewData(data).then(() => { })

这是我对AngularJS的要求:

 for (var i = 0, len = self.Scope.data.length; i < len; i++) 
{

         var data = self.Scope.data[i];
         var self = this;
//First asynchronous function
self.EcritureService.createNewData(data).then(() => {
         })                                                     
//Second asynchronous function
self.OperationService.getOperation(data.idOperation).then((operation) => {

         })   
//Third asynchronous function
self.AccountService.getAccount(data.codeCompte).then((compte) => {
               currentAccount = compte;
               currentAccount.montant = currentAccount.montant+data.montant;
         })   
//Fourth one relies on the third result, So it must be executed sequentially
self.AccountService.updateAccount(currentAccount).then(() => {
         })                    
}
// After all fetch loop's promises are resolved I want to execute some instructions here for to update the operation retrieved in the second function.
for(变量i=0,len=self.Scope.data.length;i{
})                                                     
//第二异步函数
self.OperationService.getOperation(data.idOperation)。然后((operation)=>{
})   
//第三异步函数
self.AccountService.getAccount(data.codeCompte).then((compte)=>{
活期账户=compte;
currentAccount.montant=currentAccount.montant+data.montant;
})   
//第四个依赖于第三个结果,因此必须按顺序执行
self.AccountService.updateAccount(currentAccount).then(()=>{
})                    
}
//在所有fetch循环的承诺都得到解决之后,我想在这里执行一些指令来更新在第二个函数中检索到的操作。

我希望这个循环迭代器等到所有承诺都得到解决后再继续下一步,而不是确保所有工作都完成,移动到位于循环区之外的最后一个功能,创建承诺数组,并在所有承诺都得到解决时使用
$q.all(承诺数组)

// map array of `$q.all()` promises
let promises = self.Scope.data.map((data) => {
  //First asynchronous function
  let promise_1 = self.EcritureService.createNewData(data).then(() => {})
  //Second asynchronous function
  let promise_2 = self.OperationService.getOperation(data.idOperation).then((operation) => {

  })
  //Third asynchronous function
  let promise_3 = self.AccountService.getAccount(data.codeCompte).then((compte) => {
    currentAccount = compte;
    currentAccount.montant = currentAccount.montant + data.montant;
    return currentAccount;
  }).then((currentAccount) => {
    //return promise of 4th inside `then()` of third
    return self.AccountService.updateAccount(currentAccount).then(() => {});
  });

  // this `$q.all()` resolves when this mapped instance of above all resolve
  return $q.all([promise_1, promise_2, promise_3]);

});

// resolves when all the loop instances of `$q.all()` resolve
$q.all(promises).then(()=>{
  // run completion code here
}).catch(err=>{
  console.log('Something failed in promise chain')
})

首先,我要说的是,你可能不希望这些服务成为承诺,因为你正试图绕过它们的“承诺”。因此,最简单的解决方案是重写服务以正常返回,而不是通过承诺

但要回答你的问题。第二部分第一-将第四个承诺与第三个承诺联系起来的最简单方法就是将其放在第三个承诺中。然后,像这样:

//Third asynchronous function 
self.AccountService.getAccount(data.codeCompte).then((compte) => {
           currentAccount = compte;
           currentAccount.montant = currentAccount.montant+data.montant; 
           //Fourth one relies on the third result, So it must be executed sequentially
           self.AccountService.updateAccount(currentAccount).then(() => {
     })  
 })
如果在中放入任何错误处理,则可能会决定将嵌套的承诺放在.finally子句中


至于让循环等待,for循环实际上并不是设计用来等待的。实现这一点的最简单方法是编写自己的循环,并使用$q.all将承诺组合在一起。您需要跟踪一个循环计数器,它在.then的$q.all中递增,并使用一个递归函数,当所需的循环数完成时,该函数就会出现。

Ha-这太棒了:)数组$q.all就是$q.all'ed-喜欢它:)比递归循环好得多!你能修复你的缩进吗?JavaScript是单线程的。
for
循环将在解决任何承诺之前完成。不能让循环等待。您可以像同步一样编写逻辑,将其放入函数中,然后通过同步执行器执行该函数。请参见类似示例中的函数“process()”: