Last active
August 29, 2015 14:00
-
-
Save rmcvey/11056698 to your computer and use it in GitHub Desktop.
Promises-based AJAX transactions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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