首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Async.mapLimit的Async.reflect

使用Async.mapLimit的Async.reflect
EN

Stack Overflow用户
提问于 2017-10-17 16:29:50
回答 1查看 94关注 0票数 0

我想收集从尝试保存数据到集合的所有错误(如果有的话),但它永远不会在回调中完成。有人能帮帮忙吗?

代码语言:javascript
复制
async.mapLimit(results, 5, async.reflect((result, callback) => {
  debugLog('Checking for repeating data');
  return HistoryModel.find({
      gameId: result.gameId,
      endDate: result.endDate
    })
    .then(response => {
      if (!response || response.length === 0) {
        debugLog('Saving data to history collection');
        return HistoryModel.create(result)
          .then(() => callback())
          .catch(err => callback(err, null));
      } else {
        debugLog('Data already exists');
        return callback(
          errorResponse('result',
            `The data with ${result.gameId} and ${result.endDate} already exists`), null);
      }
    })
}, (err, results) => {
  console.log(err);
  console.log(results);

  res.status(200).send(results);
}));
EN

回答 1

Stack Overflow用户

发布于 2017-10-17 16:38:00

如果既没有调用debugLog('Saving data to history collection');也没有调用debugLog('Data already exists');,那么HistoryModel.find可能已经失败了。

我会将.catch移到promise链的末尾,并对errorResponse返回的错误执行throw,最后在catch中完成整个错误处理。

代码语言:javascript
复制
async.mapLimit(results, 5, async.reflect((result, callback) => {
  debugLog('Checking for repeating data');
  return HistoryModel.find({
    gameId: result.gameId,
    endDate: result.endDate
  })
  .then(response => {
    if (!response || response.length === 0) {
      debugLog('Saving data to history collection');
      return HistoryModel.create(result)
             .then(() => callback())
    } else {
      debugLog('Data already exists');
      throw errorResponse('result',
                          `The data with ${result.gameId} and ${result.endDate} already exists`)
    }
  })
  .catch(err => { // catch all error that happen in the Promise chain
    callback(err, null)
  })
}, (err, results) => {
  console.log(err);
  console.log(results);

  res.status(200).send(results);
}));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46785773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档