Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <strong>is</strong> the most efficient model).</p> <p>So what does it mean? It means that once the synchronous code is running <strong>it is impossible</strong> for other code to be running parallely. So for example in this code</p> <pre><code>var req = http.request(options); req.end(); req.on(...); </code></pre> <p>the request is scheduled, but the main thread (i.e. the operation) hasn't ended at <code>req.end()</code>. As long as the main operation has not finished no asynchronous code can fire in-between. In particular the handler is <strong>always</strong> set before the actual event has any chance to occure.</p> <p>One more example to make it a bit clearer. Consider this code:</p> <pre><code>var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); while(true) { } // &lt;------ infinite loop doing nothing }).listen(1337, '127.0.0.1'); </code></pre> <p>Note that the first request will finish with success. But any other request will <strong>never</strong> get a response. This is because the loop will never finish the operation and JavaScript <strong>cannot</strong> 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. :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload