【发布时间】:2017-06-01 08:14:54
【问题描述】:
这是我对 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.
我希望这个循环迭代器等到所有承诺都解决后再继续下一步,而不是确保所有工作都已完成,移至位于循环块之外的最后一个功能
【问题讨论】:
-
你能请修正你的缩进吗?
-
JavaScript 是单线程的。
for循环将在解决任何承诺之前完成。不能让循环等待。 -
您可以将逻辑编写为同步的,将其放入函数中,然后通过同步执行器nsynjs 执行该函数。请参阅此处类似示例中的函数“process()”:github.com/amaksr/nsynjs/blob/master/examples/…
标签: javascript angularjs promise angular-promise