我期待循环通过蓝鸟一些任务,只是使用超时作为实验机制。 [不希望使用异步或任何其他库]循环通过任务瀑布 - 承诺蓝鸟
var Promise = require('bluebird');
var fileA = {
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five'
};
function calculate(key) {
return new Promise(function (resolve, reject) {
setTimeout(function() {
resolve(fileA[key]);
}, 500);
});
}
Promise.map(Object.keys(fileA), function (key) {
calculate(key).then(function (res) {
console.log(res);
});
}).then(function() {
console.log('finish');
});
结果是
finish,
one,
two,
three,
four,
five,
我需要循环只迭代一次,每次为完成超时,那么火与完成最后thenable。
如果Promise.map工作如何,我认为它在蓝鸟,一个简单的'return'应该修复它(之前'计算(键)。然后(函数(RES){') – orhanhenrik