与forEachSeries嵌套循环,同时

时间:2016-05-05 09:54:06

标签: node.js async.js

我正在使用forEachSerieswhilst,如下所示:

async.forEachSeries(BJRegion, function (region, key, callback) {
  var count = 0;

  async.whilst(
    function () { return count < 10; },
    function (cb) {
      // sth...
    },
    function (err, count) {             
      console.log(err, count);                              
    }
  ); // whilst
}); // forEachSeries

但是,似乎第一个while循环完成时,外部forEach不会转到下一个元素。如果没有whilstforEach将遍历BJRegion数组中的每个元素。

1 个答案:

答案 0 :(得分:0)

async.whilst退出时,您不会调用外部回调。你需要像

这样的东西
async.forEachSeries(..., function(item, next) {
  async.whilst(
    function() { ... },
    function(callback) {
      // do some work
      // ...

      // continue to the next "whilst" iteration
      callback();
    },
    function(err) {
      // "whilst" is finished!
      // continue to the next "forEachSeries" iteration
      next();
    }
  )
});

请参阅此demo