Why does a while loop block the event loop?
<p>The following example is given in a Node.js book:</p>

<pre><code>var open = false;

setTimeout(function() {
  open = true
}, 1000)

while (!open) {
  console.log('wait');
}

console.log('open sesame');
</code></pre>

<p>Explaining why the while loop blocks execution, the author says:</p>

<blockquote>
  <p>Node will never execute the timeout callback because the event loop is
  stuck on this while loop started on line 7, never giving it a chance
  to process the timeout event!</p>
</blockquote>

<p>However, the author doesn't explain why this happens in the context of the event loop or what is really going on under the hood.</p>

<p>Can someone elaborate on this? Why does node get stuck? And how would one change the above code, whilst retaining the <code>while</code> control structure so that the event loop is not blocked and the code will behave as one might reasonably expect; wait
will be logged for only 1 second before the <code>setTimeout</code> fires and the process then exits after logging 'open sesame'.</p>

<p>Generic explanations such as the answers <a href="https://stackoverflow.com/questions/14795145/how-the-single-threaded-non-blocking-io-model-works-in-node-js">to this question</a> about IO and event loops and callbacks do not really help me rationalise this. I'm hoping an answer which directly references the above code will help.</p>