SoniCoder
SoniCoder

Reputation: 4174

Possibly unhandled Error when returning promise inside a promise

I have the following code:

SuccessfulPromise().then(function() {
    return rejectedPromise();
}).catch(function(err) {
    console.log(err);
});

Is it intentional that i get the above error message or is it an error? The error gets handled in the catch block and although this i get the error message Possibly unhandled Error.

Upvotes: 0

Views: 812

Answers (2)

Avaq
Avaq

Reputation: 3031

You said:

The problem was with sinon.stub().returns(Promise.rejected('error')). The promise got executed before assigned to an error handler.

I'd like to point out that this is correct. When a promise is rejected, Bluebird checks the chain to see if any error handlers will handle it, and if not it will trigger the possiblyUnhandledRejection event. So if the rejection happens before having assigned a handler, you get a "false report".

However! There is a reason it's called possibly: The system still detects handling of that error once you do assign a handler to it, and it will then trigger an UnhandledRejectionHandled event as documented. You can use the latter event to ensure that you are not bothered with false unhandled rejections past the point of handling them.

Upvotes: 2

SoniCoder
SoniCoder

Reputation: 4174

The problem was with sinon.stub().returns(Promise.rejected('error')). The promise got executed before assigned to an error handler.

Upvotes: 1

Related Questions