 <p>This is a good use case for <em>promises</em>. </p> <p>They work like this (example using jQuery promises, but there are other APIs for promises if you don't want to use jQuery):</p> <pre><code>function doCallToGoogle() { var defer = $.Deferred(); callToGoogleServicesThatTakesLong({callback: function(data) { defer.resolve(data); }}); return defer.promise(); } /* ... */ var responsePromise = doCallToGoogle(); /* later in your code, when you need to wait for the result */ responsePromise.done(function (data) { /* do something with the response */ }); </code></pre> <p>The good thing is that you can chain promises:</p> <pre><code>var newPathPromise = previousPathPromise.then( function (previousPath) { /* build new path */ }); </code></pre> <p>Take a look to: </p> <ul> <li><a href="http://documentup.com/kriskowal/q/" rel="nofollow">http://documentup.com/kriskowal/q/</a></li> <li><a href="http://api.jquery.com/promise/" rel="nofollow">http://api.jquery.com/promise/</a></li> </ul> <p>To summarize promises are an object abstraction over the use of callbacks, that are very useful for control flow (chaining, waiting for all the callbacks, avoid lots of callback nesting).</p>