-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Node stopping execution prematurely even though promise is still pending. #25504
Comments
FWIW node master, v10.x, and v8.x all behave the same. |
@ealmansi Do you mean that an unresolved Promise alone should keep the process running? That would be a significant breaking change and hard to implement (Promises are a language feature, the event loop a Node.js/libuv one). I think somebody has floated the idea of (optionally?) giving information on unresolved promises when the process exits. I’m not sure what that would look like, but maybe this would help you? |
@mscdex @addaleax Thanks for your replies. Turns out I was working on the wrong assumption that Node would never normally exit the process if there were any pending promises. I now understand that this is not the case. I got this misunderstanding from the fact that this very often seems to be the actual behaviour, but in reality Node only waits on promises to get resolved when they are wrapping something like a timeout or a network or IO request. |
Just for future reference: there's nothing about an unresolved promise that would actually keep a process running. They aren't executing any code or waiting for anything, they're just a fancy way to call a callback. |
I found out this issue as I was having the same assumption of @ealmansi, but I want to exemplify that this problem may be quite difficult to figure out when you combine promises, events and async/await features. Take this code: const { EventEmitter } = require('events');
const emitter1 = new EventEmitter();
const emitter2 = new EventEmitter();
emitter2.on('data', () => emitter1.emit('data', 'emitted'));
const promise = new Promise((resolve) => {
emitter1.on('data', (value) => {
console.log('emitted');
resolve();
});
}).then(() => console.log('finished'));
async function waitPromise() {
console.log('starting');
await promise;
console.log('awaited');
}
waitPromise(); Output:
I know it's working as intended but, because of the language features used, especially the async/await, is quite counterintuitive to figure out what's happening, but I have no idea how it could behave differently, as async/await is just syntax sugar. |
Upon executing the following code, Node finishes running even though a pending promise is left over.
I get the following output, and then the Node process stops running.
I would expect Node to get stuck and run forever, since the promise in the example will never resolve. When running this in Firefox, I get indeed a never-resolving promise.
Additional note: this looks like a contrived toy example, but is actually just a minimal reproduction of a problem I have in a real codebase.
The text was updated successfully, but these errors were encountered: