I have this very confusing snippet of code using es6 async
await
syntax. What I would expect to happen is that the process hangs on the await
line forever, since the resolve function is never called. However, what actually happens is that "start" is outputted and then the process exits with no more output.
const simple = async () => {
console.log('start')
await new Promise(resolve => {})
console.log('done.')
}
simple()
this code below however, will print "start", wait 1 second, and the print "done."
const simple = async () => {
console.log('start')
await new Promise(resolve => setTimeout(resolve, 1000))
console.log('done.')
}
simple()
My closest guess to what this means (without any evidence) is that while node is waiting on a promise, it keeps track of the active things happening in your code, when there is nothing else happening, it simply exits. Can someone explain why the code exits here?
running node v8.7.0