Complicated use case for Node.js Async Module

蹲街弑〆低调 提交于 2019-12-01 09:20:38

Try using the auto method.

Determines the best order for running the functions in tasks, based on their requirements. Each function can optionally depend on other functions being completed first, and each function is run as soon as its requirements are satisfied.

Functions also receive an object containing the results of functions which have completed so far.

For example:

async.auto({
  A: function (callback) {
    callback(null, 'A data');
  },
  B: ['A', function (results, callback) {
    // do something with results.A;
    callback(null, 'B data');
  }],
  C: ['A', function (results, callback) {
    // do something with results.A;
    callback(null, 'C data');
  }],
  D: ['A', 'B', function (results, callback) {
    // do something with results.A and results.B;
    callback(null, 'D data');
  }],
  E: ['A', 'C', function (results, callback) {
    // do something with results.A and results.C;
    callback(null, 'E data');
  }]
}, function (err, results) {
  console.log('err = ', err);
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!