Why is "timeout' printed by node before terminating when I execute the following code ?
const shouldNotPrint = async () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('timeout')
resolve('foo')
}, 300)
})
}
shouldNotPrint()
console.log('ended')
Running the previous code with node 16.13.0 will output
> node src/index.js
ended
timeout
Is there some kind of top-level await in node waiting for all promises to resolve before ending the process ?
To clarify: I do not want to terminate Node before the end of my promise. I just want to understand, at a low level, the behavior of node and the event-loop in that case. I understand the logic of a timeout + promise and why it would be printed after "ended". The question is why it is displayed at all and why Node is waiting for it to be displayed before terminating the process.