简体   繁体   中英

Combining async.each and async.series

I want to combine async.each and async.series but I'm getting unexpected results.

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(){

}); 

I would expect this code to output 123123 . Instead I'm getting 112233 . What am I doing wrong?

async.each() applies the function iterator to each item in array in parallel . If you want to do it serially, you should use eachSeries() .

Additionally, You should use the final callback in 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(){

}); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM