我正在寻找蓝鸟的一些任务,只是使用超时作为实验机制.[不希望使用异步或任何其他库]
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');
});
Run Code Online (Sandbox Code Playgroud)
结果是
finish,
one,
two,
three,
four,
five,
Run Code Online (Sandbox Code Playgroud)
我需要循环只在每个超时完成后迭代一次,然后用完成后触发最后一个.