jQuery has a nice feature of its Deferred
API, $.wait()
for working with multiple Deferred
s / Promise
s. It returns when:<
i had the same problem lately: I think that occurs because $.when
is more of a Promise.all
implementation.
I wasn't able to find a Promise.allSettled
alternative in the jQuery documentation and across the web so here's my attempt to get it:
const deferredsAllSettled = deferreds => {
const settlements = [];
deferreds.forEach(deferred => {
const settlement = jQuery.Deferred();
deferred.always(settlement.resolve);
settlements.push(settlement);
});
const returnedDeferred = $.Deferred();
jQuery.when
.apply(jQuery, settlements)
.then(() =>
returnedDeferred.resolve(
deferreds.map(deferred => deferred.promise())
)
);
return returnedDeferred.promise();
};
NOTE: I've written that for a utils file so the Promise.allSettled
one-only iterable parameter signature was ok for me, if you want to write it in the $.when
spirit (passing and getting the $.Deferred
s as separate arguments) you'll have to write a little more...