什么是node.js最好的控制stream模块?

我已经使用了caolan的asynchronous模块 ,这是非常好的,但是跟踪错误和通过控制stream传递数据的不同方式导致开发有时是非常困难的。

我想知道是否有更好的select,或目前在生产环境中使用什么。

谢谢阅读。

我也使用asynchronous。 为了帮助跟踪错误,build议您为函数命名,而不是加载匿名函数:

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

这样,您将在堆栈跟踪中获得更多有用的信息。

…但是跟踪错误以及通过控制stream量传递数据的不同方式导致开发有时非常困难。

我最近创build了一个名为“wait.for”的简单抽象,以同步模式调用asynchronous函数(基于Fibers): https : //github.com/luciotato/waitfor

使用wait.for,你可以使用'try / catch',同时仍然调用asynchronous函数,并保持函数作用域(不需要closures)。 例:

 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 }; 

请参阅https://github.com/luciotato/waitfor上的示例

有时很难把所有的function放在一个数组中。 当你有一个对象的数组,并想要为每个对象做一些事情,我使用类似下面的例子。

阅读更多: 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); }); 

我命名函数,因为它更容易debugging(并且更易于阅读)