I've seen some places in unit tests where a function returns a q.allSettled()
promise from a function call and there's a .fail
attached to the promise. But .allSettled
will call .then
even if some/all promises are rejected.
Here's an example:
function someFunctionToTest(){
var aRejectedDeferred = Q.defer();
var anotherRejectedDeferred = Q.defer();
var aResolvedDeferred = Q.defer();
aRejectedDeferred.reject(new Error("I'm aRejectedPromise"));
anotherRejectedDeferred.reject(new Error("I'm anotherRejectedPromise"));
aResolvedDeferred.resolve({awesome:"I'm aResolvedPromise"});
return Q.allSettled([aRejectedDeferred.promise,anotherRejectedDeferred.promise, aResolvedDeferred.promise])
}
it('should never fail', function(done) {
someFunctionToTest()
.then(function (data) {
should.equal(data[0].state,'rejected');
should.equal(data[1].state,'rejected');
should.equal(data[2].state,'fulfilled');
// throw(new Error('I will trigger fail!!!'));
done();
})
.fail(function (err) {
done(err);
});
});
Can you provide some examples where .fail
would be triggered?
Promise.all fail-fast behaviorPromise.all is rejected if any of the elements are rejected. For example, if you pass in four promises that resolve after a timeout and one promise that rejects immediately, then Promise.all will reject immediately.
Promise. allSettled() will never reject - instead it will wait for all functions passed in the array to either resolve or reject.
allSettled(promises) is a helper function that runs promises in parallel and aggregates the settled statuses (either fulfilled or rejected) into a result array.
The Promise. allSettled() method returns a promise that fulfills after all of the given promises have either fulfilled or rejected, with an array of objects that each describes the outcome of each promise.
Q.allSettled
cannot fail. See Q API which says
Returns a promise that is fulfilled with an array of promise state snapshots, but only after all the original promises have settled, i.e. become either fulfilled or rejected.
This is quite clear that whatever promises in allSettled
do you will still get a resolved promise.
In your particular case the .then
statement may fail because of the e.g. following line
should.equal(data[0].state,'rejected');
This may throw an assertion exception which will lead to the fail and you can catch it and run done();
which is the way to say to the testing tool that asynchronous request has been finished. But this is just a guess. Q.allSettled
itself cannot fail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With