转换为async.waterfall

时间:2017-07-20 14:58:00

标签: javascript asynchronous

我有以下代码

function someHelper(someList) {
  return someList.map((item) => {
    return (next) => someService.firstCall(paramForFirst, (err, result) => {
      if(err) next(err);
      else    someService.secondCall(paramForSecond, result, next);
    })
  });
}

module.exports = {
  doSomething(param, callback) {
    async.parallel(someHelper(someList), callback);
  }
};

如何将someHelper函数的第二次返回转换为使用async.waterfall

1 个答案:

答案 0 :(得分:0)

这是我的解决方案。从并行传递next(回调),然后调用瀑布数组列表中的方法,最后调用next。通过定义waterfall将调用带有结果或错误的回调。

function someHelper(someList) {
  return someList.map((item) => {
    return (next) => {
      async.waterfall([
        async.apply(someService.firstCall, paramForFirst),
        async.apply(someService.secondCall, paramForSecond)
      ], next);
    }
  });
}