public
Created

Async in jQuery with nested $.when

  • Download Gist
async.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
function A(){
promises1 = new Array();
promises2 = new Array();
for (var i = start; i < end; i++)
{
B(someUri);
}
await();
}
 
function await()
{
$.when.apply($, promises1).always(function() {
$.when.apply($, promises2).always(function() {
doSomethingToTheDataStoredInSomeGlobalVariableBelow();
});
});
}
 
function B(uri){
promises1.push($.getJSON(uri, function(data){
someCollectionFrom_data.each(function () {
promises2.push($.get(someUriFrom_this, function(data){
extractSomeStuffFrom_data_AndStoreInSomeGlobalVariable();
}));
}
));
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.