Let's say I have an array of so-called ajax calls:
var callsArray = [];
callsArray.push(
$.ajax({
url: '/url1';
cache: false,
dataType: 'html'
}),
$.ajax({
url: '/url2';
cache: false,
dataType: 'html'
})
)
I know in advance that at least one of these two calls will fail. I want to execute the function after the BOTH calls resolved both with success and with an error, and I also want to log any failures.
This will not work:
var errors = '';
$.when.apply(null, callsArray)
.done(function() {
console.log('Both calls completed but these urls failed: ' + errors);
})
.fail(function() {
errors += this.url + ' ';
})
The problem with the above is that .fail is executed if even one call fails, and .done is only executed if null calls fail. Also I can not use. Always, because it is executed as soon as any call is resolved.
So I'm looking for something like this:
var errors = '';
$.when.apply(null, callsArray)
.allCallsResolved(function() {
console.log('All calls completed but these urls failed: ' + errors);
})
.everyFailure(function() {
errors += this.url + ' ';
})
source
share