Promise vs setTimeout โ interactive event loop example
1console.log('start');2 3setTimeout(() => {4 console.log('timeout');5}, 0);6 7Promise.resolve().then(() => {8 console.log('promise');9});10 11console.log('end');The script starts executing top to bottom on the call stack. console.log("start") runs synchronously and prints right away.
1 / 9
Call stack
script
Web APIs โ browser land
The event loop โ is the call stack empty? โ drain the microtask queue first โ then take one callback.
Microtask queue โ priority
Callback queue
Console
01start
Fig. โ Promise vs setTimeout ยท step 1 of 9
โ โ step ยท space play ยท 123 level