Consider this:
JavaScript
x
6
1
(async function () {
2
const arr = [];
3
await new Promise(r => arr.push(r));
4
console.log("done")
5
})();
6
The scripts terminates at the await
and the log is never printed to sdout, I don’t understand why
Advertisement
Answer
Nothing is wrong with your code. That’s just the model of Node. If there is no pending I/O pending promises don’t prevent Node from exiting.
You can schedule some I/O if you want to stop Node from exiting but pending promises don’t actually prevent Node from exiting.
I’m not saying I agree or like the behavior but that’s what we currently do 🤷
Edit: found the bug report/discussion in the issue tracker.