The event loop
Understand the idea
A resolved logs before a zero-delay timer, even though the timer was registered first. They wait in different queues.
One scheduler, one executing stack
A browser needs to respond to clicks, wait for network work, and run your code without letting two callbacks change the same state on the same JavaScript thread at once. The event loop coordinates when queued work can start. It does not make slow code harmless.
The call stack and browser work
Synchronous calls occupy the stack until they return. Browser can arrange future work while that code runs. A timer waits outside the stack, then makes a task eligible. Registering it does not place its callback immediately on the stack.
The call stack records the currently executing. A call adds a frame; returning removes it. Browser APIs can manage a timer or request outside that stack, then arrange for JavaScript to handle the result later. The browser doing work elsewhere is different from two JavaScript functions executing simultaneously on this stack.
| Place | What belongs there | When it can move on |
|---|---|---|
| Call Stack | The synchronous functions running now | As calls return or throw |
| Web APIs | Browser-managed waiting such as a timer delay | When the browser makes the callback eligible |
| Microtask Queue | Promise reactions and queueMicrotask callbacks | At a checkpoint after current work finishes |
| Macrotask / Task Queue | Eligible timer callbacks and other tasks | After the checkpoint, when the loop takes a task |
Microtasks before the next task
The reactions registered on a use the microtask queue. Once synchronous work finishes at a checkpoint, the browser runs queued microtasks before moving to the next task. Microtasks queued by another microtask are processed during that same checkpoint. A continuously replenished microtask queue can therefore delay timers and rendering.
A task, often called a macrotask in teaching material, can run a timer callback or an event handler. Promise reaction callbacks join the microtask queue. When the current synchronous work finishes, the browser reaches a microtask checkpoint and drains those microtasks before taking the next task. A microtask that adds more microtasks can keep that checkpoint busy.
Why zero delay loses the race
This is why a resolved promise usually wins the familiar comparison with a zero-delay timeout. Both are registered during the script, the script finishes, the promise reaction runs as a microtask, and the timer callback runs as a later task. “Macrotask” is a common teaching name for tasks such as timers.
Read the observed order
Microtasks created by other microtasks are also drained at that checkpoint. An endless chain can starve rendering and other tasks. Browsers have several task sources and choose rendering opportunities between work; the four-lane model here explains this specific ordering without claiming that every browser event shares one universal FIFO queue.
Predict four logs: start, end, promise, timer. Registering the timer first does not put it ahead of the promise reaction, because they use different queues. The animation below follows those locations one step at a time. It models this small browser scenario; real browsers also schedule rendering and have multiple task sources.
Mental model
Call Stack
Web APIs
Microtask Queue
Macrotask Queue
Synchronous work occupies the call stack.
01 / 11Start the script
Live example
Predict start → end → → timer. The animation tracks each through its own lane before it reaches the stack.
Your console output appears here.
Guided challenge
Label the queue correctly
Restore the promise callback’s label so the output identifies the true microtask-before-timer order.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
start end promise timer
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.