Doing some testing, I need to wait for a couple of promises to be rejected.
I know that I can use jQuery.when() to wait for several promises tu be resolved. But that method reject the master promise as soon one of the promises fails.
I know that all my promises will fail, but I need to wait for it anyway. How can I do it?
In a kind of pseudo-code what I want to do is:
var promise1 = connection.doCall();
var promise2 = connection.doCall();
$.when([promise1, promise2]).allOfThemFail(function() {
assertThatSomeProcessWasDoneOnlyOnce()
});
Using the Bluebird promise library, you can use
Promise.settle()
which waits for all the promises to be settled (e.g. fulfilled or rejected) and then you can simply query the list to see if they were all rejected.Here's a jQuery-specific version of
settle()
:And, you would use this similarly to the Bluebird solution:
And, here's a
settle()
type function build from scratch using standard ES6 promises. It returns a single promise that is resolved when all the other promises have finished (regardless of their final state). The resolved result of this uber promise is an array ofPromiseInspection
objects where you can queryisFulfilled()
orisRejected()
and can obtain the.value()
or.reason()
.And, you would use this similarly as above:
I don't think there is a built in function for that, but you can easily implement