Waiting for multiple asynchronous JSON queries to finish before running code <p>I am looking to run multiple different JSON request simultaneously, and then only perform a function once they have either completed or returned an error message (404 etc.). Here's what I have so far, but the function <code>finished</code> doesn't remember the variables from either request. Any ideas?</p> <pre><code>function socialPosting(a_date,a_title,a_source,a_thumbnail,a_url) { socialPosts[socialPosts.length] = {date: a_date, title: a_title, source: a_source, thumbnail: a_thumbnail, url: a_url}; //console.log(socialPosts[amount]); //console.log(amount); } function finished(source) { if (source == "Twitter") { var Twitter = "True"; } if (source == "YouTube") { var YouTube = "True"; } console.log(source); //diagnostics console.log(Twitter); //diagnostics console.log(YouTube); //diagnostics if (Twitter == "True" &amp;&amp; YouTube == "True") { console.log(socialPosts[0]); //Should return after both requests are complete } } $.getJSON('https://gdata.youtube.com/feeds/api/videos?author=google&amp;max-results=5&amp;v=2&amp;alt=jsonc&amp;orderby=published', function(data) { for(var i=0; i&lt;data.data.items.length; i++) { //for each YouTube video in the request socialPosting(Date.parse(data.data.items[i].uploaded),data.data.items[i].title,"YouTube",data.data.items[i].thumbnail.hqDefault,'http://www.youtube.com/watch?v=' + data.data.items[i].id); //Add values of YouTube video to array } finished("YouTube"); }); $.getJSON("https://api.twitter.com/1/statuses/user_timeline/twitter.json?count=5&amp;include_rts=1&amp;callback=?", function(data) { var amount = socialPosts.length; for(var i=0; i&lt;data.length; i++) { socialPosting(Date.parse(data[i].created_at),data[i].text,"Twitter"); //Add values of YouTube video to array } finished("Twitter"); }); </code></pre>