What's the most efficient way to wait for an asynchronous function call in Javascript?
<p>This might be the opposite of <a href="http://stackoverflow.com/questions/9614953/javascript-how-to-write-a-function-that-will-be-executed-asynchronously">my previous question here</a> but anyway, I need to know its answer as well.</p>

<p>I have an Ajax call in my algorithm that I need to wait for it to run some code. There are two solutions:</p>

<p>1) The typical solution:</p>

<pre><code>ajaxCall(function(result){
  //the code to run after the call is returned
});
</code></pre>

<p>2) The one I'm wondering if it can be an alternative:</p>

<pre><code>res=null;
ajaxCall(function(result){
  res=result;
});
while(res==null)/*wait! but this will lock the GUI I guess*/;
//do the rest of the code because now res is initialized
</code></pre>

<p>The question is how can I write the second solution in an efficient way that doesn't freeze the GUI?</p>
