Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best control flow module for node.js?

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.

like image 212
Robin Duckett Avatar asked Aug 05 '11 10:08

Robin Duckett


People also ask

What is control flow in node JS?

The control flow is the order in which the computer executes statements in a script. Code is run in order from the first line in the file to the last line, unless the computer runs across the (extremely frequent) structures that change the control flow, such as conditionals and loops.

What is node js best suited for?

Node. js is primarily used for non-blocking, event-driven servers, due to its single-threaded nature. It's used for traditional web sites and back-end API services, but was designed with real-time, push-based architectures in mind.


2 Answers

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.

like image 190
evilcelery Avatar answered Oct 01 '22 01:10

evilcelery


...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

like image 21
Lucio M. Tato Avatar answered Oct 01 '22 01:10

Lucio M. Tato