$.whenAll
is an extension to jQuery's $.when
. The main difference is $.when
stops when it receives the first rejected promise
. This extension treats all successes and failures as progress events. After all the promises have completed, the global promise is resolved if there were no errors. Otherwise the global promise is rejected.
$.whenAll($.get('http://github.com'), $.get('good luck with this one'))
.then(
// success callback
function(request) {}
// failure callback - called once at the end
,function() {}
// progress callback - called twice (before the failure callback)
,function(data, state, jqXhr) {
// $.ajax().done/fail so the params are conditional based on a success or failure
// The second param is consistently the state of the request.
if (state == 'sucess') {
// data is the data returned as expected
// jqXhr is what you expect
}
else {
// data is actually the jqXhr object for failed requests
// jqXhr is the text of the error "Not Found" in this example
}
}
)
;
var baseUrl = 'http://magic.value/here/'.
$.whenAll($('input.foo')
.map(function() {
return $.get(baseUrl + this.value);
})
.get()
)
.progress(function() {
// update visuals to show this failed
})
.failed(function() {
// reset state to allow the user to resubmit
})
;
Awesome, solved my problem and worked great!