【问题标题】:nesting loop with forEachSeries and whilst together使用 forEachSeries 和 while 嵌套循环
【发布时间】:2016-05-11 07:29:15
【问题描述】:

我将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 数组中的每个元素。

【问题讨论】:

    标签: node.js async.js


    【解决方案1】:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多