19

I've used caolan's async module which is very good, however tracking errors and the varying way of passing data through for control flow causes development to sometimes be very difficult.

I would like to know if there are any better options, or what is currently being used in production environments.

Thanks for reading.

Robin Duckett
  • 2,094
  • 3
  • 16
  • 15

3 Answers3

18

I use async as well. To help tracking errors it's recommended you name your functions, instead of having loads of anonymous functions:

async.series([
  function doSomething() {...},
  function doSomethingElse() {...},
  function finish() {...}
]);

This way you'll get more helpful information in stack traces.

evilcelery
  • 15,941
  • 8
  • 42
  • 54
  • Thanks, this has helped. Do you know of any others that are good? – Robin Duckett Aug 09 '11 at 16:28
  • Another one I've used is [Step](https://github.com/creationix/step) which is pretty good. I think async has more options that may better suit different situations. – evilcelery Aug 10 '11 at 10:21
4

...however tracking errors and the varying way of passing data through for control flow causes development to sometimes be very difficult.

I've recently created a simple abstraction named "wait.for" to call async functions in sync mode (based on Fibers): https://github.com/luciotato/waitfor

Using wait.for, you can use 'try/catch' while still calling async functions, and you keep function scope (no closures needed). Example:

function inAFiber(param){
  try{
     var data= wait.for(fs.readFile,'someFile'); //async function
     var result = wait.for(doSomethingElse,data,param); //another async function
     otherFunction(result);
  }
  catch(e) {
     //here you catch if some of the "waited.for" 
     // async functions returned "err" in callback
     // or if otherFunction throws
};

see the examples at https://github.com/luciotato/waitfor

Lucio M. Tato
  • 5,639
  • 2
  • 31
  • 30
  • Elegant. Fibers sacrifices a little portability. But this is one of the most readable [solutions](https://github.com/joyent/node/wiki/modules#user-content-wiki-async-flow) I have seen, with no need for a pre-processor. Great job! – joeytwiddle May 30 '14 at 20:39
-1

Sometimes it is hard to put all the functions in an array. When you have an array of objects and want to do something for each object, I use something like the example below.

read more in: http://coppieters.blogspot.be/2013/03/iterator-for-async-nodejs-operations.html

 var list = [1, 2, 3, 4, 5];
 var sum = 0;

 Application.each(list, function forEachNumber(done) { 
   sum += this; 

   // next statement most often called as callback in an async operation
   // file, network or database stuff

   done(); // pass an error if something went wrong and automatically end here

 }, function whenDone(err) { 
   if (err) 
     console.log("error: " + err);
   else
     console.log("sum = " + sum); 

});

I name the functions, because it is easier to debug (and easier to read)

Johan
  • 53
  • 1
  • 9