Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Q.allSettled ever fail?

Tags:

node.js

promise

q

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?

like image 435
Shanimal Avatar asked Apr 26 '16 17:04

Shanimal


People also ask

What happens to Promise all if one fails?

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.

Does Promise allSettled throw?

Promise. allSettled() will never reject - instead it will wait for all functions passed in the array to either resolve or reject.

Does Promise allSettled run in parallel?

allSettled(promises) is a helper function that runs promises in parallel and aggregates the settled statuses (either fulfilled or rejected) into a result array.

How does Promise allSettled work?

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.


1 Answers

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.

like image 67
smnbbrv Avatar answered Sep 30 '22 19:09

smnbbrv