async.map没有't使用嵌套瀑布调用cb

async.map doesn't call cb with a nested waterfall

本文关键字:布调用 调用 cb 嵌套 没有 map async      更新时间:2023-09-26

我在异步方面遇到了一些问题。

这是我的代码:

Main.prototype._onlyGeoLinks = function() {
  var redis = RedisClient.client
    , getKeys = function(key, cb) {
      redis.keys(key, cb);
    }
    , count = 0
  //
  cb = null;
  async.waterfall([
    function(cb) {
      async.concat([
        'events:*'
        , 'news:*:*:*:*'
        , 'deals:*'
      ], getKeys, cb);
    },
    function(keys, cb) {
      // iterate keys
      async.map(keys, function(key, cb) {
        async.waterfall([
          function(cb) {
            redis.get(key, cb);
          }, function(item, cb) {
            log.verbose(LOG_CTX, 'Parsing item.. %d', ++count);
            GeoHelper.parse(item, cb); // This are calling cb(new Error('bla'))
          }
        ], cb) // at this point, I can read err
      }, cb) // This is never called
    }
  ], function(err, items) {
    if (err) {
      log.error(LOG_CTX, err);
    } else {
      log.verbose(LOG_CTX, items);
      log.verbose(LOG_CTX, items.length);
    }
  })
}

我尝试过的

async.map(keys, function(key, cb) {
    async.waterfall([
      function(cb) {
        redis.get(key, cb);
      }, function(item, cb) {
        log.verbose(LOG_CTX, 'Parsing item.. %d', ++count);
        GeoHelper.parse(item, cb);
      }
    ], function(err) {
      log.error('1', err);  // This prints ERR
      cb(err);
    })
  }, function(err) {
    log.error('2', err);  // this is never called
    cb(err);
  })

我错过了什么?谢谢你的帮助。

我发现,如果迭代器在映射过程中调用cb时出错,则会调用主cb,但映射会继续。这就是为什么我没有看到错误,输出被有效响应的输出覆盖。

如果迭代器将错误传递给此回调,则主回调(映射函数)被立即调用,并出现错误

异步#映射