I have a strange javascript behaviour that I cannot explain. When I run
const waitFor = (ms) => new Promise(r => setTimeout(r, ms))
// const o = 2;
[1, 2, 3].forEach(async (num) => {
await waitFor(50);
console.log(num);
});
console.log('Done');
It only logs Done
. This is expected since forEach
cannot get async callbacks.
However if you uncomment the line const o = 2
(o
is not used), it logs
Done
1
2
3
Can you explain why?