-
Notifications
You must be signed in to change notification settings - Fork 29.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
A strange behavior of Node.js: program exit 0 inside try{}
block without run finally {}
block
#19929
Comments
Unresolved promises do not keep a process alive. In this case, You could also think of the code in your example in slightly different terms: function main() {
return new Promise(() => console.log('IN TRY BLOCK'))
.finally(() => console.log('IN FINALLY BLOCK'));
}
main(); (This is actually not quite correct as there should be a nested Promise but then reasoning about it becomes a pain.) |
In general, I recommend https://github.com/nodejs/help/issues for these types of questions. This tracker is for Node.js core issues or bugs only. |
@apapirovski Thanks for the reply. I can understand that the However, my question is: should we keep the Because this behavior of the |
@zixia This isn't really a Node.js issue, it's a language issue (EcmaScript) and I can't imagine it will change given that it would break the entirety of the rest of the JavaScript ecosystem. |
Yes, I agree with you that it's not a Node.js issue but an ES language issue. Thank you very much! |
If you want to think about this in C++ or Java terms, it's as if you sent the thread a |
@bnoordhuis Brilliant! 👍 I feel better about this Node.js behavior after your explanation: It's fair enough for the program languages when running on the operation system with some extreme conditions... |
Recently I find we can skip the
finally {}
block when we are inside atry{}
block and without any error reported.The shortest code is as the following:
After running the above program, it will exit immediately with the following one line output:
If we
echo $?
we can get0
. Thefinally{}
block has been skipped(which is unexpected).After thinking/asking/thinking, I begin to understand the reason behind this, more details are in this StackOverflow question/answer: https://stackoverflow.com/questions/49728838/node-js-program-does-not-run-the-finally-block-after-try-and-exit-with/
However, I still feel there's something not right.
So I post this issue is want to know: do we think this behavior is right? I feel it is not expected because if we write a
try {} finally {}
, we will expect thefinally {}
block always be executed, if it is not at least should raise an error, and I believe the Java/C++ will all follow this flow.The text was updated successfully, but these errors were encountered: