开发者

Why is waterfall so slow?

I'm using async module (see https://github.com/caolan/async) for Node.js and my question is... Why is waterfall so slow?

It takes about 4 seconds to execute this piece of code...

App.post("/form", function(request, response) {

Async.waterfall([

  function(callback) {

    console.log("1.");
    callback开发者_如何学Python(null, "some data");

  },

  function(data, callback) {

    console.log("2.");            
    callback(null, "some data");

  },

  function(data, callback) {

    console.log("3.");
    callback(null, "some data");              

  }

], function(error, document) {

  console.log("4.");            
  console.log("Done.");

  response.send(); // Takes 4 seconds

});

}

Output

1.
2.
// After 4 seconds
3.
4.
Done.

Thanks for reply!


It's just another Node.js Bug.

Using process.nextTick inside another process.nextTick during a pending http.ServerResponse is broken.

var http = require('http');
http.createServer(function(req, res) {
    var now = new Date();
    process.nextTick(function() {
        process.nextTick(function() {
            console.log(new Date() - now);
            res.writeHead({});
            res.end('foooooo');
        });
    });
}).listen(3000);

This takes an eternity, async.js calls the callbacks from inside the other callbacks which were called via process.nextTick which then results in the above bug being triggered.

Quick fix: In async.js line 63 modifiy async.nextTick to only use setTimeout.

Bug: I've filed an issue on this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜