for (var i in listofInstances) {
cleanupInstance(listofInstances[ i ])
.then(function () {
console.log("Done" + listofInstances[ i ])
});
}
cleanupInstance
也是一个 promise 链。然而,目前我的 for 循环在整个 promise 链完成之前进入下一次迭代。有没有办法也 promise 循环?我正在使用 Bluebird 库 (nodejs) 来实现 promise 。
最佳答案
你可以使用 .each
:
var Promise = require('bluebird');
...
Promise.each(listofInstances, function(instance) {
return cleanupInstance(instance).then(function() {
console.log('Done', instance);
});
}).then(function() {
console.log('Done with all instances');
});
关于javascript - for 循环中的 Promise 链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32075877/