You may already be familiar with why this doesn't work, and there are excellent explanations in an oft linked duplicate. However, this isn't entirely a duplicate. Because you're not asking this:
How do I return the response from an asynchronous operation?
You're asking something more like this:
How do I loop over a bunch of asynchronous operations and wait for them all to finish?
For that, you'd first need to put them into an array. Something like this:
var my_promises = [];
var data_num = [];
$.each(arr_urls, function( index, value ) {
my_promises.push($.getJSON(arr_urls[index], function (data) {
data_num.push(data);
}));
});
This should give you an array called promises
which holds references to the currently-executing asynchronous operations. Now you just need to wait for them all to complete. Something like this:
$.when.apply($, my_promises).then(function() {
alert(data_num);
});
This should apply the .when()
function to the entire array of promises, executing the callback only when that entire array has completed.