ProgramGuru

Understanding the Event Loop

Understanding the Event Loop — the core idea

The event loop coordinates the call stack and queued callbacks. Promise reactions enter the microtask queue; timer callbacks are scheduled later through host tasks. Their ordering matters when diagnosing UI behavior and asynchronous tests.

What you’ll learn

  • Explain understanding the event loop with a concrete use case.
  • Run the example and account for every line of its output.
  • Recognize the difference between compile-time checking and runtime behavior.

Understand the rules and when to use them

Reading the code: A and B run on the initial stack. The Promise reaction is processed as a microtask after that stack empties. The zero-delay timer is eligible only on a later task turn; it prints last.

Keep the boundary clear: setTimeout(fn,0) is not a promise that fn will run immediately or at an exact wall-clock time.

See the idea as a flow

Understanding the Event Loop — conceptual flowstack A→B then microtask then timer taskUnderstanding the Event Loop1stack A→B2microtask3timer taskAsynchronous operations may resume after the current JavaScript stack.
Follow the three stages and identify what happens in TypeScript versus JavaScript.

Run the complete TypeScript example

Save the code as event-loop.ts. The result below was captured after compiling this exact example with strict TypeScript checking and executing the emitted JavaScript in Node.js.

TypeScript code

console.log("A");
setTimeout(() => console.log("timer"), 0);
Promise.resolve().then(() => console.log("microtask"));
console.log("B");

Verified output

A
B
microtask
timer

Build: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable event-loop.ts; run: node event-loop.js. The exact filename may be changed freely. When a multi-file project exists, prefer its tsconfig.json and build scripts.

Trace the example line by line

A and B run on the initial stack. The Promise reaction is processed as a microtask after that stack empties. The zero-delay timer is eligible only on a later task turn; it prints last.

The output shown above comes from the initialized values in this program. Replacing a type annotation alone does not transform a JavaScript value.

Avoid this common mistake

setTimeout(fn,0) is not a promise that fn will run immediately or at an exact wall-clock time.

Check both the TypeScript diagnostic and the emitted JavaScript behavior. A program can compile correctly and still need runtime validation or error handling.

Practice independently

Add a second Promise.resolve().then(...) and predict its position before the timer.

Before running your change, write down the type you expect TypeScript to infer and the output you expect JavaScript to print. Then check both predictions.

Check your understanding

After A and B, which callback runs first?
Choose one answer. You can try again.

Answer and explanation

The Promise microtask, after synchronous code and before the timer.

A and B run on the initial stack. The Promise reaction is processed as a microtask after that stack empties. The zero-delay timer is eligible only on a later task turn; it prints last.

What to remember

The event loop coordinates the call stack and queued callbacks.

Try the practice change and re-run the exact program to test your understanding.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Promises in TypeScript →