title: node.js: while loop callback not working as expected
body: <p>Knowing that while Node.js is working asynchronously, writing something like this:</p>
  
  <pre><code>function sleep() {
      var stop = new Date().getTime();
      while(new Date().getTime &lt; stop + 15000) {
          ;
      }
  }
  
  sleep();
  console.log("done");
  </code></pre>
  
  <p>...would call the sleep(), block the server for the duration of the while loop (15secs) and just THEN print "done" to the console. As far as I understand, this is because Node.js is giving JavaScript only access to the main thread, and therefore this kidn of thing would halt further execution.</p>
  
  <p>So I understand the solution to this is to use callbacks:</p>
  
  <pre><code>function sleep(callback) {
      var stop = new Date().getTime();
      while(new Date().getTime() &lt; stop + 15000) {
          ;
      }
      callback();
  }
  
  sleep(function() {
      console.log("done sleeping");
  });
  
  console.log("DONE");
  </code></pre>
  
  <p>So I thought this would print 'DONE' and after 15 secs. 'done sleeping', since the sleep() function gets called and is handed a pointer to a callback function. While this function is working (the while loop), the last line would be executed (print 'done'). After 15 seconds, when the sleep() function finishes, it calls the given callback function, which then prints 'done sleeping'.</p>
  
  <p>Apparently I understood something wrong here, because both of the above ways block. Can anybody clarify please?</p>
  
  <p>Thanks in advance,
  Slagjoeyoco</p>