I was just trying to make a simple async function that runs on a schedule. I was just starting off with a counter. But this loop is behaving differently from how I imagined.
async function runCheckChats(){
while(true){
count++
console.log(`this loop is running`);
await new Promise(res => {
setInterval(() => {
count++
res(3);
}, 3000)
})
console.log(`count = ${count}`)
}
}
runCheckChats();
The first time the loop runs, count is increased by one. The second time, it's increased by two, and so on. Why is this happening? When we await a promise, wouldn't we be exiting runCheckChats in the event loop? Why does it seem like the while keeps going instead of waiting? I would expect it to count every 3 seconds.