Skip to content

Instantly share code, notes, and snippets.

@rmcvey
Last active August 29, 2015 14:00
Show Gist options
  • Save rmcvey/11056698 to your computer and use it in GitHub Desktop.
Save rmcvey/11056698 to your computer and use it in GitHub Desktop.
Promises-based AJAX transactions
var endpoints = ['/data/step1', '/data/step2', '/data/step3'];
// this will hold our promises
var promises = [];
$.each(endpoints, function(index, url){
// push the returned promise from $.get to our array
promises.push($.get(url));
})
// $.when takes a list of promises, we will use apply to pass an arbitrary number of arguments
$.when.apply($, promises).done(function(){
console.log('Promise is complete')
}).fail(function(jqXHR, textStatus, errorThrown){
console.log('Not all conditions were met');
console.log("Error: %s (%s)", textStatus, errorThrown);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment