What is the exact output and event sequence of this code?
Tests event loop priority between sync, microtasks, and macrotasks. A strong answer gives Start, End, Promise, Timeout; explains Promise.then is a microtask and setTimeout is a macrotask, microtasks draining before the next macrotask.
WHAT THIS TESTS: Your understanding of the HTML event loop processing model, specifically the distinction between synchronous execution, microtasks, and macrotasks. The interviewer wants to see if you can predict execution order based on how the browser queues work, not just memorize the output. This is fundamental to writing non-racy asynchronous code, debugging timing issues in production, and understanding why seemingly immediate callbacks are deferred.
A GOOD ANSWER COVERS: First, state the exact output in order: Start, End, Promise, Timeout. Second, explain that console.log Start and End run synchronously on the call stack during the initial script evaluation. Third, explain that Promise.resolve().then registers a microtask via HostEnqueuePromiseJob, while setTimeout registers a macrotask via HostEnqueueTimeoutJob. Fourth, describe the event loop processing model: after the current task finishes, the browser performs a microtask checkpoint, draining all microtasks before rendering or selecting the next macrotask from a task queue. Therefore Promise prints before Timeout even though both were scheduled during the same synchronous block. Fifth, mention that the zero in setTimeout is a minimum delay, not a guarantee, and that the timer task must wait for the current task and any microtasks to complete.
COMMON WRONG ANSWERS: Saying the output is Start, End, Timeout, Promise because setTimeout was called with zero milliseconds. Claiming that microtasks and macrotasks are interchangeable or execute in FIFO order across queue types. Describing setImmediate or process.nextTick behavior from Node.js instead of browser semantics. Saying the promise callback runs synchronously inline. Asserting that setTimeout guarantees exact timing rather than minimum delay. Confusing the job queue from the JavaScript specification with the browser event loop task queues.
LIKELY FOLLOW-UPS: What happens if you add a second Promise.then or a queueMicrotask call? Where do MutationObservers fit in? How does this change if the code runs inside an async function with await? What is the impact on UI rendering if a microtask queue contains an infinite loop? How do task priorities differ across browsers? Can you starve the event loop with microtasks?
ONE CONCRETE EXAMPLE: Imagine a React component that calls setState inside a Promise.then and also logs from a setTimeout. If a developer assumes the timeout runs first because it was scheduled earlier in the function, they might write race-sensitive cleanup logic that fires in the wrong order, leaving dangling subscriptions or stale closures in the component. Understanding the microtask checkpoint prevents off-by-one-frame bugs in animations and ensures state updates flush before timers inspect the DOM.
Read the original → html.spec.whatwg.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.