小编Joh*_*ell的帖子

循环任务瀑布 - 承诺蓝鸟

我正在寻找蓝鸟的一些任务,只是使用超时作为实验机制.[不希望使用异步或任何其他库]

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)

我需要循环只在每个超时完成后迭代一次,然后用完成后触发最后一个.

javascript asynchronous node.js promise bluebird

7
推荐指数
1
解决办法
3526
查看次数

标签 统计

asynchronous ×1

bluebird ×1

javascript ×1

node.js ×1

promise ×1