Behavior of empty promises in Node.js

Elena Sharovar
1 min readMay 12, 2021

--

Dear Node.js I did not expect this:

  • if we do await [number] output is as expected:
(async function main() {
console.log('STARTED')
await 123;
console.log('AWAIT DONE')
console.log('DOING SOMETHING ELSE')
})()
STARTED
AWAIT DONE
DOING SOMETHING ELSE
  • if we do await [null] output is as expected:
(async function main() {
console.log('STARTED')
await null;
console.log('AWAIT DONE')
console.log('DOING SOMETHING ELSE')
})()
STARTED
AWAIT DONE
DOING SOMETHING ELSE
  • if we do await [undefined] output is as expected:
(async function main() {
console.log('STARTED')
await undefined;
console.log('AWAIT DONE')
console.log('DOING SOMETHING ELSE')
})()
STARTED
AWAIT DONE
DOING SOMETHING ELSE
  • but if we do await for some promise which is never resolved, Node.js just silently exits with exit code `0` (success) just after await!
(async function main() {
console.log('STARTED')
await new Promise(resolve => {});
console.log('AWAIT DONE')
console.log('DOING SOMETHING ELSE')
})()
STARTED
exited with code 0

lines after “await” are never executed.

Node.js just silently exited with exit code `0` (success) just after await!

This is weiiiiiiirrrrrd!

--

--