Let's say I have an array of ajax calls like so:
// an array of 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 a function after BOTH calls have resolved as either succeeded or failed, and I also want to log any failures.
This won't work:
// NOT APPROPRIATE
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 executes if even one call fails, while .done only executes if zero calls fail. Also I can't use .always because it executes as soon as any call resolves.
Therefore I'm looking for something like this:
// FANTASY CODE
var errors = '';
$.when.apply(null, callsArray)
.allCallsResolved(function() {
// this executes only when all calls have
// returned either a success or a failure
// rather than when all calls succeed
console.log('All calls completed but these urls failed: ' + errors);
})
.everyFailure(function() {
// this executes every time a call fails
// rather than as soon as any call fails
errors += this.url + ' ';
})