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

后端 未结 3 1584
醉梦人生
醉梦人生 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

    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)

提交回复
热议问题