Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Event Loop in JavaScript

Trace the event loop in JavaScript: synchronous logs run first, then a promise microtask, then a timer. A zero delay does not mean immediate execution.

AdvancedAdvanced JavaScriptExample 11 of 12

Run this example once in a fresh browser console. The console shows the printed results.

event-loop.js
1console.log('Start revision');
2setTimeout(() => console.log('Timer reminder'), 0);
3Promise.resolve().then(() => console.log('Saved-note microtask'));
4console.log('Finish current script');

Sample Output

Start revision, then Finish current script

Both synchronous logs run before any queued callback.

Saved-note microtask, then Timer reminder

The promise reaction runs at the microtask checkpoint before the eligible timer task.

In this one

  • Trace synchronous lines first
  • Distinguish a timer from a promise reaction
  • Understand zero-delay scheduling
  • Predict a small browser log sequence

How it works

  1. 1

    Print Start revision immediately.

  2. 2

    Schedule a timer without running its callback now.

  3. 3

    Schedule a then handler on an already-resolved promise.

  4. 4

    Print Finish current script before queued callbacks.

  5. 5

    Run the promise microtask, then the timer task, giving the four-line output shown.

Edge cases

Long synchronous loop

Queued callbacks wait while the current JavaScript keeps running.

A timer does not interrupt a long loop. Break up heavy work or consider an appropriate worker for costly calculations.

Zero delay

setTimeout with zero does not promise a zero-time response.

The callback must wait for a task turn and can be delayed by browser scheduling.

What's going on

The event loop is the browser's process for choosing when queued work gets a turn to run. Queued work means functions waiting for later execution. This example shows why a zero-delay timer does not interrupt the code already running.

The current script prints Start revision, schedules a timer, schedules a promise reaction, and prints Finish current script. A promise represents a value that may be available now or later. Even when it is already resolved, its then handler runs later as a microtask. A microtask is a small queued job processed after the current JavaScript work finishes and before the next ordinary task is selected.

The timer callback is an ordinary task. It gets a turn after the promise microtask here, so Timer reminder is last. Zero is the requested minimum delay, not a guarantee of immediate execution. Busy work can postpone both callbacks. This example describes browser ordering for these scheduled jobs; it does not say every kind of background activity runs on the same thread or every environment has identical queues.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Which line prints second?

Pick an answer to check it.

Question 2

What does a zero-delay timer do?

Pick an answer to check it.

Questions people ask

Why does Finish current script appear before the promise?

then callbacks are queued. They do not interrupt the current script even for a resolved promise.

Why is the timer last?

In this example the promise microtask runs at the checkpoint before the next timer task is handled.

Does this mean the timer is broken?

No. A timer schedules later work after at least a delay; it is not an exact real-time alarm.