Hello I am relatively new to javascript and node.js and I was looking into how the event loop worked as well as asynchronous programming When I ran this code
function time(ms){
return new Promise((resolve) =>{
setTimeout(resolve,time)
})
}
async function foo(){
await time(2000)
console.log("hi ")
await time(2000)
console.log("hello")
await time(1000)
console.log("bar")
}
foo()
console.log("a")
setTimeout(()=> console.log("b"),10)
The output was
a hi hello b bar
when I expected it to be
a b hi hello bar
Then when I changed the last line to
setTimeout(()=> console.log("b"),3000)
I thought the output would be
a hi b hello bar
but it was
a hi hello bar b
Now I think there's something about the event loop that I'm not understanding because based on my understanding of the task queue and call stack my outputs should be correct but they aren't.
setTimeout(resolve, ms), notsetTimeout(resolve, time)