这是我与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.
我想这个循环迭代等到所有的承诺才去下一步不是确保所有被解决作品完成后,移动到驻留在环集团外
你能*请*修理你的缩进? – Bergi
JavaScript是单线程的。在任何**的承诺解决之前,for循环将完成**。循环不能等待。 – georgeawg
你可以编写你的逻辑,就好像它是同步的,放到函数中,并通过同步执行器[nsynjs](https://github.com/amaksr/nsynjs)执行该函数。在类似的例子中看到函数“process()”:https://github.com/amaksr/nsynjs/blob/master/examples/browser-ajax-seq.js – amaksr