【问题标题】:Combining async.each and async.series结合 async.each 和 async.series
【发布时间】:2015-12-11 14:49:13
【问题描述】:

我想将 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。我做错了什么?

【问题讨论】:

    标签: javascript asynchronous callback async.js


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-05-24
      • 2013-03-29
      • 1970-01-01
      • 2012-03-04
      • 2014-10-22
      • 1970-01-01
      • 2016-05-17
      • 2015-02-18
      • 2012-06-02
      相关资源
      最近更新 更多