【问题标题】:async.map doesn't call cb with a nested waterfallasync.map 不使用嵌套瀑布调用 cb
【发布时间】:2014-04-24 00:18:24
【问题描述】:

我在使用 async 时遇到了一些问题。

这是我的代码:

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

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

【问题讨论】:

    标签: javascript async.js


    【解决方案1】:

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

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

    Async#map

    【讨论】:

      猜你喜欢
      • 2018-12-28
      • 2023-03-15
      • 1970-01-01
      • 2014-05-09
      • 2012-08-23
      • 2013-12-31
      • 1970-01-01
      • 2017-01-24
      • 2017-03-02
      相关资源
      最近更新 更多