合并 async.each 和 async.series

Combining async.each and async.series

我想合并 async.each 和 async.series,但我得到了意想不到的结果。

async.each([1, 2], function(item, nloop) {
    async.series([
        function(callback) {
            console.log("1");
            callback();
        },
        function(callback) {
            console.log("2");
            callback();
        },
        function(callback) {
            console.log("3");
            callback();
        },
        function(callback) {
            nloop();
        }
    ]);
},function(){

}); 

我希望此代码输出 123123。 相反,我得到 112233。我做错了什么?

async.each()applies the function iterator to each item in array in parallel。 如果你想连续做,你应该使用 eachSeries().

此外,您应该在 async.series(taskArray, callback) 中使用最终回调:

async.eachSeries([1, 2], function(item, nextItem) {
    async.series([
        function(next) {
            console.log("1");
            next();
        },
        function(next) {
            console.log("2");
            next();
        },
        function(callback) {
            console.log("3");
            next();
        }
    ], nextItem);
},function(){

});