Unhandled rejection occurs when not chained
See original GitHub issue- What version of bluebird is the issue happening on? 3.5.0
- What platform and version? (For example Node.js 0.12 or Google Chrome 32) Chrome 58 - jsfiddle
- Did this issue happen with earlier version of bluebird? Yes
It seems that chaining then, catch, and finally properly handles rejection, but when the calls to then, catch, and finally are not chained an unhandled rejection is thrown. I have a jsfiddle here.
var promiseCBs = [];
var promise1 = new Promise((resolve, reject) => {
promiseCBs.push({resolve, reject});
});
var promise2 = new Promise((resolve, reject) => {
promiseCBs.push({resolve, reject});
});
promise1.then(() => console.log('a'));
promise1.catch(() => console.log('b'));
promise1.finally(() => console.log('c'));
promise2.then(() => console.log('1'))
.catch(() => console.log('2'))
.finally(() => console.log('3'));
//unhandled rejection
promiseCBs[0].reject(new Error('rej0'));
//no unhandled rejection
promiseCBs[1].reject(new Error('rej1'));
Issue Analytics
- State:
- Created 6 years ago
- Comments:8
Top Results From Across the Web
Unhandled Rejection Error - why? // Promise Chain Design ...
When you chain the catch directly to promise , it does not apply to the promise that is coming back from the then...
Read more >Node incorrectly throws ERR_UNHANDLED_REJECTION on ...
In your particular case the code is not ok since .then(someFn) creates a rejected promise chain and the rejection isn't handled. You can...
Read more >Creating a JavaScript promise from scratch, Part 7: Unhandled ...
Rejected promises without rejection handlers can hide important errors, which is why both Node.js and browsers have a way of tracking them.
Read more >Microtasks - The Modern JavaScript Tutorial
An “unhandled rejection” occurs when a promise error is not handled at the end of the microtask queue. Normally, if we expect an...
Read more >Tracking unhandled rejected Promises - 2ality
One risk is that rejections may get lost, leading to silent failures. ... Node.js does not report unhandled rejections, but it emits events ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Interesting, I didn’t realize this previously. Thanks for the knowledge!
Multicast semantics are fundamental to promises, this is part of the spec and you can create a model that only allows one continuation (in JS those are typically called “tasks”).
Multicast semantics are fundamental in the design of promises. In practice if promises were unicast then things like unhandled rejections and cancellation become a lot simpler at the expense of the end user.