I was expecting the following snippet to hang forever since the promise is never neither fulfilled nor rejected, instead the JavaScript runtime is simply terminated and the code after the await
is never executed.
(async function () {
console.log('so far so good');
await new Promise(() => {});
console.log('never printed');
})();
What's worse is that the above also happens dynamically, that is when the Promise becomes impossible to fulfill/reject after a certain event, for example the following only keep the runtime alive for one second:
(async function () {
console.log('so far so good');
await new Promise((fulfill, reject) => {
const timeout = setTimeout(fulfill, 2000);
setTimeout(() => {
clearTimeout(timeout);
}, 1000);
});
console.log('never printed');
})();
This is counterintuitive to say the least, what's the rationale behind this? More importantly can you link me to a document/spec that deals with this behavior?