Pergunta

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(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!

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top