gpt4 book ai didi

javascript - for 循环中的 Promise 链

转载 作者:数据小太阳 更新时间:2023-10-29 05:54:27 27 4
gpt4 key购买 nike

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com