SO - 36276384
by Arun P Johny
JavaScript
function allCompleted(array) {
var deferred = jQuery.Deferred(),
counter = array.length,
results = [];
$.each(array, function(i, item) {
item.always(function() {
results[i] = [].slice.call(arguments, 0)
if (--counter == 0) {
deferred.resolveWith(undefined, results);
}
});
});
return deferred.promise();
}
var l1 = $.ajax({
url: '/echo/html/',
type: 'post',
data: {
html: '<div>d</div>'
}
});
var l2 = $.ajax({
url: '//placehold.it',
type: 'post',
data: {
html: '<div>d</div>'
}
});
allCompleted([l1, l2]).done(function(r1, r2) {
console.log('always', r1)
console.log('always', r2)
})