-
Notifications
You must be signed in to change notification settings - Fork 30.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Promise .finally gives UnhandledPromiseRejection even if .catch has been used. #28636
Comments
That is how promises work. You defined const p = Promise.reject(new Error('BOOM'));
p.catch(() => {}).finally(() => {}); Similar question was asked before: nodejs/help#1208 |
Catch is simply to catch the errors. |
@Hakerh400's explanation is correct. |
@targos I just ran into this and it was a really nasty surprise, had me staring at my screen thinking wtf for 30 minutes, even though I've been using promises for years and years. I think the intended behavior could be changed to be more humane. Is there no way that node could internally flag the promise returned by If there's no |
I don't know if Node.js could do something like that, but I find the behavior consistent with the equivalent synchronous code: try {
throw new Error('boom')
} finally {
console.log('hello')
} This will log "hello" and also leave an unhandled exception. |
@targos yes I know, but consider this difference -- unlike sync code where you only need to catch an error once, with async code you can end up having to catch the same error multiple times to prevent unhandled rejection, even if only one of those catch handlers actually needs to do anything about it. I realize now, the problem can happen with Because if I create a dead-end |
Version: v12.6.0 (but 10+ also has same behaviour)
Platform: Linux sf2 4.15.0-52-generic #56-Ubuntu SMP Tue Jun 4 22:49:08 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
Subsystem:Promises
If one use promise.finally(..), it leads to unhandled promise rejection warning despite the .catch(..) handler also used.
Example code:
const p = Promise.reject(new Error('BOOM'));
p.catch(() => {});
p.finally(() => {});
Output:
(node:22503) UnhandledPromiseRejectionWarning: Error: BOOM at Object.<anonymous> (/home/user/other/t1/t.js:25:26) at Module._compile (internal/modules/cjs/loader.js:776:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10) at Module.load (internal/modules/cjs/loader.js:643:32) at Function.Module._load (internal/modules/cjs/loader.js:556:12) at Function.Module.runMain (internal/modules/cjs/loader.js:839:10) at internal/main/run_main_module.js:17:11 (node:22503) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3) (node:22503) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
If you comment last line with .finally handler, no warnings issued.
The text was updated successfully, but these errors were encountered: