I'm having trouble understanding how process.nextTick does its thing. I thought I understood, but I canβt reproduce how I feel this should work:
var handler = function(req, res) { res.writeHead(200, {'Content-type' : 'text/html'}); foo(function() { console.log("bar"); }); console.log("received"); res.end("Hello, world!"); } function foo(callback) { var i = 0; while(i<1000000000) i++; process.nextTick(callback); } require('http').createServer(handler).listen(3000);
While foo is executing the loop, I will send several requests, assuming that the handler will queue several times behind foo with a callback in the queue only after foo completes.
If I'm right how this works, I assume that the result will look like this:
received received received received bar bar bar bar
But it is not, it is simply consistent:
received bar received bar received bar received bar
I see that foo returns before making a callback , which is expected, but it seems that the callback is NEXT in the line, and not at the end of the queue, for all incoming requests. what does it work? Perhaps I just donβt understand how the event queue in node works. And please do not point me here . Thanks.
alf
source share