script ends despite unresolved promise

Tags: , , ,



Consider this:

(async function () {
  const arr = [];
  await new Promise(r => arr.push(r));
  console.log("done")
})();

The scripts terminates at the await and the log is never printed to sdout, I don’t understand why

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.



Source: stackoverflow