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

后端 未结 3 1583
醉梦人生
醉梦人生 2021-02-18 22:43

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 ver

3条回答
  •  日久生厌
    2021-02-18 22:59

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

提交回复
热议问题