javascript - for 循环中的 Promise 链

标签 javascript node.js promise bluebird

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/

相关文章:

javascript - 渲染数百万个元素的最佳 WebGL 框架

javascript - 如何防止兄弟状态 Controller 刷新?

php - Javascript 有问题

node.js - Heroku 上的 nodemon 启动错误

javascript - 了解 Promise 构造函数

javascript - 如何正确实现 .then promise

javascript - Angular promise .finally() 被乱序调用

javascript - dojo contentPane,我在我的 ContentPane 中发布带有 href 的 html 代码。 html代码有脚本但不起作用,为什么?

node.js - "This site can’ t be reached"using Nginx as reverse proxy for Express Nodejs

javascript - Node.js Passport OAuth 2.0 身份验证 : where to store access and refresh tokens