Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced20 minLESSON 23 / 34

The event loop

01

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.

Where each piece of pending work belongs
PlaceWhat belongs thereWhen it can move on
Call StackThe synchronous functions running nowAs calls return or throw
Web APIsBrowser-managed waiting such as a timer delayWhen the browser makes the callback eligible
Microtask QueuePromise reactions and queueMicrotask callbacksAt a checkpoint after current work finishes
Macrotask / Task QueueEligible timer callbacks and other tasksAfter 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.

02

Mental model

Call Stack

script()

Web APIs

Microtask Queue

Macrotask Queue

OUTPUT

Synchronous work occupies the call stack.

01 / 11Start the script

03

Live example

Predict start → end → → timer. The animation tracks each through its own lane before it reaches the stack.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Label the queue correctly

Restore the promise callback’s label so the output identifies the true microtask-before-timer order.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

start
end
promise
timer

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What runs after the current script in this example?
    02Can an already fulfilled promise interrupt the current synchronous statement?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson