title: Force a javascript code to be synchronous
body: <p>I have an asynchronous funcion (closing a socket) that I have to execute when the user leaves the page. I've tried to do it through jQuery :</p>
  
  <pre><code>$(window).unload(function() {
     socket.disconnect();
  });
  </code></pre>
  
  <p>However, this doesn't work because the function is asynchronous and the client leaves the page as soon as it reach the end of the method, that's to say instantly and the request to disconnect the socket is not performed.</p>
  
  <p>We can provide a callback executed at the end of the <code>socket.disconnect()</code> function execution. Maybe this could help...</p>
  
  <p>Is there a way in javascript to solve this ?</p>