JavaScript runs on a single thread — one call stack, doing one thing at a time. So how does it juggle timers, promises, clicks and network replies without ever waiting? A tiny dispatcher called the event loop keeps the stack fed: when it goes empty, the loop pulls the next ready piece of work back onto it. Scroll through to watch synchronous code, timers and promises take their turns — in exactly the order the real runtime would run them.
The runtime has a call stack (what's running now), Web APIs
that handle timers/I/O off to the side, and two waiting lines: the macrotask queue
(e.g. setTimeout callbacks) and the microtask queue (e.g. Promise.then).
The rule the loop follows: run all synchronous code, then — only when the stack is empty — drain
every microtask, then take one macrotask, then drain microtasks again. Step through the program
below or press Auto-play and watch the output order build.
"Asynchronous" does not mean "in parallel". There is still just one thread. A queued callback can only run once the stack is empty — so a long synchronous loop hogs the stack and makes everything else (timers, clicks, even repainting) wait its turn. Press Run heavy loop and watch the whole machine freeze; the spinner stops, queued callbacks pile up, and the UI is unresponsive until the loop finishes.
One-line contrast: cooperative single-thread concurrency = one worker who never stops but only ever holds one task at a time; true threads = several workers running genuinely at once (e.g. Web Workers / OS threads).
queueMicrotask) before taking the next macrotask
(setTimeout, I/O, UI events). That's why a Promise.then logged before a
setTimeout(…,0) still runs first.