This post helps me understand the reason why but it's a little brief on the solution.
The documentation mentions the callback functions .done()
, .fail()
and .always()
but I've tried .done() and couldn't get my variable outside the call.
Here is my relevant code:
var countries = [];
$.getJSON("js/flags.json", function(data) {
$(data.flags).each(function(i) {
countries.push(data.flags[i]);
});
console.log(countries); // returns a big ol array :)
});
console.log(countries); // returns an empty array :(
I just want to be able to use this array globally. What am I missing?