3

I have started using Node.js as my backend for performing different operations like DB queries/ API calls, etc. I was reading about Node.js Async and decided to give it a try. It has been working for simple use cases where I want some tasks in parallel or series but somehow I have landed in a requirement where I need an optimal combination of series/parallel/waterfall techniques of Async.

Use Case:

I have following functions implemented with callbacks:

function A(input, callback) {
...
callback(err,result);
}


function B(input1, input2, callback) {
...
callback(err,result);
}


function C(input, callback) {
...
callback(err,result);
}


function D(input, callback) {
...
callback(err,result);
}


function E(input, callback) {
...
callback(err,result);
}

And their order of execution should be like:

A -> [B -> D]
A -> [C -> E]

  • B,D,C,E are executed after A.
  • D is executed after B
  • E is executed after C
  • B is executed after A
  • C is executed after A

  • B,D and C,E are not dependent on one another, so they can be executed in paralled after A has completed execution.

  • D is dependent on B and should be executed after B

  • E is dependent on C and should be executed after C.

Also, I need to pass data from each of the functions to their dependent functions. So, A should be paasing result to B and C. Similarly B passing to D and C passing to E.

How can I execute them efficiently using Async module ?

I think I need something like but not sure though:

async.waterfall(    
  A: ...,    
  async.parallel(
      async.waterfall (
          B,
          D
      ),
      async.waterfall (
          C,
          E
      )
   )
) 
halfer
  • 19,824
  • 17
  • 99
  • 186
dark_shadow
  • 3,503
  • 11
  • 56
  • 81
  • 3
    Why not switch to a standard based implementation -> ES6 Promises? – Amit Aug 15 '15 at 20:33
  • Hi there. We try to encourage questions that are as succinct as possible. Thus, if you are in the habit of putting 'Thanks', 'Please help me' etc in your questions, it's worth knowing these are generally thought of as noise, and are often edited out. – halfer Aug 15 '15 at 22:55
  • 1
    @halfer: I'll keep that in mind for future posts. – dark_shadow Aug 16 '15 at 05:54

1 Answers1

4

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);
});
Community
  • 1
  • 1
Robbie
  • 18,750
  • 4
  • 41
  • 45