Back to feed
Dev.to
Dev.to
7/4/2026
The original title is "The Event Loop: Microtasks, Macrotasks & Output Prediction"

The original title is "The Event Loop: Microtasks, Macrotasks & Output Prediction"

Original: The Event Loop: Microtasks, Macrotasks & Output Prediction

Short summary

Master JavaScript's event loop — the single most-asked question in senior tech interviews at Google, Meta, and every AI company. This comprehensive guide teaches the mental model of call stacks, microtask queues (Promises, queueMicrotask), and macrotask queues (setTimeout, events), plus the ironclad priority rules to predict output for any async code. Includes WebSocket behavior, Node.js phases, Web Worker threading, worked examples, cheat sheets, and real interview questions with detailed answers.

  • Event loop priority: synchronous → ALL microtasks → ONE macrotask → repeat; microtasks always beat macrotasks
  • Promises run before setTimeout even at 0ms because microtasks drain before macrotasks execute
  • Apply this mental model to predict output of any snippet and debug race conditions in production code

Generated with AI, which can make mistakes.

Is this a good recommendation for you?

Comments

Failed to load comments. Please try again.

Explore more