嵌套节点async.eachSeries

时间:2014-09-07 17:09:04

标签: javascript node.js nested async.js

与异步模块争夺了半天但在嵌套几个级别时无法正常工作。

所以这样可行:


    var async = require('async')

    var myarr = ["Outer - A", "Outer - B"];
    var myarr2 = ["Inner - A", "Inner - B"];
    var innerComplete = true;

    async.eachSeries(myarr, function( item, outerCallback) {
        console.log('Processing item ' + item);
        async.series([
            function(callback) {
                takeTime(2000, item, callback)
            },
            function(callback) {
                takeTime(1000, item, callback)
            },
            function(callback) {


                outerCallback();
            }
        ], function(err) {
            console.log("---OUTER SEQUENCE---")

        })
    }, function(err){
        console.log("---OUTER LOOP---")

    });

    function takeTime(ms, msg, callback) {
        console.log("Starting " + ms + " ms task from " + msg);
        setTimeout( function() { 
            console.log("Finished " + ms + " ms task from " + msg);
            callback();
        }, ms);
    } 

它按顺序输出所有内容:

Processing item Outer - A
Starting 2000 ms task from Outer - A
Finished 2000 ms task from Outer - A
Starting 1000 ms task from Outer - A
Finished 1000 ms task from Outer - A
Processing item Outer - B
Starting 2000 ms task from Outer - B
Finished 2000 ms task from Outer - B
Starting 1000 ms task from Outer - B
Finished 1000 ms task from Outer - B
---OUTER LOOP---

但是当我尝试将另一个eachSeries循环嵌入其中时:


    var async = require('async')

    var myarr = ["Outer - A", "Outer - B"];
    var myarr2 = ["Inner - A", "Inner - B"];
    var innerComplete = true;

    async.eachSeries(myarr, function( item, outerCallback) {
        console.log('Processing item ' + item);
        async.series([
            function(callback) {
                takeTime(2000, item, callback)
            },
            function(callback) {
                takeTime(1000, item, callback)
            },
            function(callback) {
                async.eachSeries(myarr2, function( item2, outerCallback2) {
                    console.log('Processing item ' + item2);
                    async.series([
                        function(callback2) {
                            takeTime(2000, item2, callback2)
                        },
                        function(callback2) {
                            takeTime(1000, item2, callback2)
                        }
                    ], function(err) {
                        console.log('---INNER SEQUENCE---')

                    })
                }, function(err){
                    console.log("---INNER LOOP---")
                });

                outerCallback();
            }
        ], function(err) {
            console.log("---OUTER SEQUENCE---")

        })
    }, function(err){
        console.log("---OUTER LOOP---")

    });

    function takeTime(ms, msg, callback) {
        console.log("Starting " + ms + " ms task from " + msg);
        setTimeout( function() { 
            console.log("Finished " + ms + " ms task from " + msg);
            callback();
        }, ms);
    } 

在输入第二个eachSeries循环时丢失执行顺序,如下所示:

Processing item Outer - A
Starting 2000 ms task from Outer - A
Finished 2000 ms task from Outer - A
Starting 1000 ms task from Outer - A
Finished 1000 ms task from Outer - A
Processing item Inner - A
Starting 2000 ms task from Inner - A
Processing item Outer - B
Starting 2000 ms task from Outer - B
Finished 2000 ms task from Inner - A
Starting 1000 ms task from Inner - A
Finished 2000 ms task from Outer - B
Starting 1000 ms task from Outer - B
Finished 1000 ms task from Inner - A
---INNER SEQUENCE---
Finished 1000 ms task from Outer - B
Processing item Inner - A
Starting 2000 ms task from Inner - A
---OUTER LOOP---
Finished 2000 ms task from Inner - A
Starting 1000 ms task from Inner - A
Finished 1000 ms task from Inner - A
---INNER SEQUENCE---

我也尝试过瀑布,mapSeries等,但是使用相同或其他混乱的执行顺序。我做错了什么或者async模块是否支持这种嵌套?

1 个答案:

答案 0 :(得分:10)

你没有给outerCallback2打电话,你没有给callback打电话,而你正在立即拨打outerCallback

修正:

async.eachSeries(myarr, function( item, outerCallback) {
  ,----------------------------------------'
  |  console.log('Processing item ' + item);
  |  async.series([
  |      function(callback) {
  |                 `--------------,
  |          takeTime(2000, item, callback)
  |      },
  |      function(callback) {
  |                 `--------------,
  |          takeTime(1000, item, callback)
  |      },
  |      function(callback) {
  |     ,-----------'
  |     |     async.eachSeries(myarr2, function( item2, outerCallback2) {
  |     |    ,---------------------------------------------'
  |     |    |   console.log('Processing item ' + item2);
  |     |    |   async.series([
  |     |    |      function(callback2) {
  |     |    |          takeTime(2000, item2, callback2)
  |     |    |      },
  |     |    |      function(callback2) {
  |     |    |          takeTime(1000, item2, callback2)
  |     |    |      }
  |     |    |  ], function(err) {
  |     |    |      console.log('---INNER SEQUENCE---')
  |     |    `--->  outerCallback2(err); // <<<
  |     |       })
  |     |   }, function(err){
  |     |      console.log("---INNER LOOP---");
  |     `--->  callback(err); // <<<
  |         });
  |     }
  | ], function(err) {
  |      console.log("---OUTER SEQUENCE---")
  `--->  outerCallback(err); // <<<
    })
}, function(err){
    console.log("---OUTER LOOP---")
    console.log("everything done");
});