I'm using jQuery to run some ajax:
var stuff = {};
function do_stuff(thing){
stuff[thing.id] = thing;
}
function fun(){
var calls = [];
for(var i = 0; i < 10; i++){
var call = $.get(URL, function(data){
do_stuff(data);
});
calls.push(call);
}
$.when(calls).done(function(){
console.log(stuff);
});
}
I'm using $.when
, but what I get is {}
instead of the data that I expect. When I set a breakpoint in Chrome, the log
line gets called before any of the do_stuff
calls - so apparently the order of the calls get mixed up. Is there a way I can ensure the console.log(stuff)
line gets called after my get callbacks do?