jQuery has a nice feature of its Deferred
API, $.wait()
for working with multiple Deferred
s / Promise
s. It returns when:
- All of the
Deferred
s have beenresolve()
d
or
- One of the
Deferred
s has beenreject()
ed
Most of the time this is what you want, but sometimes you want to know when all of them have been reject()
ed.
Is there a simple or elegant way to do something like $.wait()
but only when all Deferred
s have been rejected?
(There may be other use cases but mine is to combine this with waiting for the first of several Deferred's to resolve.)
In the spirit of how the Promise specification is likely going for the future with a
PromiseInspection
object, here's a jQuery add-on function that tells you when all promises are done, whether fulfilled or rejected:Then, you can use it like this:
Keep in mind that
$.settle()
will always fulfill (never reject) and the fulfilled value is an array ofPromiseInspection
objects and you can interrogate each one to see if it was fulfilled or rejected and then fetch the corresponding value or reason. See the demo below for example usage:Working demo: https://jsfiddle.net/jfriend00/y0gjs31r/