node.js - attaching event handlers on time

前端 未结 2 560
独厮守ぢ
独厮守ぢ 2021-01-12 16:05

I\'m studying node.js and came across this example in the node.js manual:

...
var req = http.request(options);
req.end();

req.on(\'upgrade\', function(res,          


        
相关标签:
2条回答
  • 2021-01-12 16:31

    What you miss is that JavaScript isn't asynchronous at all! What I mean is that JavaScript is single-threaded and asynchronous operations are actually not asynochronous. There is a very fancy queue model which gives as the illusion of JavaScript being asynchronous (don't get me wrong: it still is the most efficient model).

    So what does it mean? It means that once the synchronous code is running it is impossible for other code to be running parallely. So for example in this code

    var req = http.request(options);
    req.end();
    req.on(...);
    

    the request is scheduled, but the main thread (i.e. the operation) hasn't ended at req.end(). As long as the main operation has not finished no asynchronous code can fire in-between. In particular the handler is always set before the actual event has any chance to occure.

    One more example to make it a bit clearer. Consider this code:

    var http = require('http');
    http.createServer(function (req, res) {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('Hello World\n');
        while(true) { } // <------ infinite loop doing nothing
    }).listen(1337, '127.0.0.1');
    

    Note that the first request will finish with success. But any other request will never get a response. This is because the loop will never finish the operation and JavaScript cannot jump to another event because of it. This code permamently crashes the app beyond any hope. So be careful with synchronous code with Node.js. :)

    0 讨论(0)
  • 2021-01-12 16:56

    The key thing to understand is that there is only a single event loop and control ONLY leaves the current "tick" of the event loop when you call an asynchronous function. Usually this happens naturally when you do I/O (every few lines of code is common) or call setTimeout/setInterval (fairly rare). So as long as all of your event handlers are registered within the same tick of the event loop, you will never loose any data. Moreover, within that tick of the event loop, it doesn't matter what order you attach your handlers, because during that tick your code is literally the only thing node is executing, so no other code can receive I/O or invoke any event handlers until the current event loop tick completes. It's not about waiting "long enough" or network latency or anything like that. It's the simplicity of a single event loop that guarantees predictable operation with regard to the event handler functions.

    0 讨论(0)
提交回复
热议问题