I make an ajax call in a loop, the ajax call is in the class self.fileUpload:
$.when.apply($, $.map($('.file-new'), function(fileNewTemplate, i) {
var formData = new FormData();
return self.fileUpload.upload(formData).then(function(data){
return $.Deferred().resolveWith(this);
}).fail(function(){
console.log('ajax call failed');
});
})).done(function(){
console.log('when done');
})
.always(function(){
console.log('when always');
})
.fail(function(){
console.log('when fail');
})
The problem is, when one ajax call fails, the fail of the when is called. I need a solution that would call done or always once all promises are returned, despite whether some or all failed.
I checked out the answers here:
$.Deferred: How to detect when every promise has been executed
And as you can see from the above code, I have implemented the accepted solution.
The problem is, done is still never called and always is called as soon as one ajax call fails.
Where am I going wrong?