I am working in node.js v18.2.0 and here is my code:
async function res_asyncf(){
await setTimeout(r => {}, 1000);
}
const res_promise = new Promise(async r => {
await setTimeout(r, 1000);
});
async function not_res_asyncf(){
while(true){ }
}
const not_res_promise = new Promise(async r => { });
(async () => {
console.log("Async wrapper entered");
await <async_thing_here>;
console.log("Promise resolved");
})();
Instead of <async_thing_here>
I was writing res_asyncf()
, res_promise
, not_res_asyncf()
and finally not_res_promise
.
The first one did not wait, which I do not understand.
The second behaved as expected: it hung for a second and then Promise resolved
was printed.
The third one also behaved as expected: it hung forever.
But the last one just did nothing and exited and even did not print Promise resolved
.
Expected: it hangs just as the third one.
Why does all this happen?