Skip to content

Instantly share code, notes, and snippets.

@chribben
Created May 26, 2012 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chribben/2795435 to your computer and use it in GitHub Desktop.
Save chribben/2795435 to your computer and use it in GitHub Desktop.
Async in jQuery with nested $.when
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();
}));
}
));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment