mediumCodingConceptMidSenior
Here is a script mixing setTimeout, a promise chain and an await. Tell me the order the logs come out in, and why.
4 min readUpdated 2026-08-15Target archetype: Big Tech, Enterprise Captive, Product Startup
Answer
Short answer
The engine finishes the current task, then drains the whole microtask queue before taking another task, so every promise callback and await resumption runs before a zero-delay timeout, in the order each was queued.
Was this answer helpful?
Likely follow-ups
- How would you process fifty thousand items without freezing the page, and what do you yield with?
- Where do requestAnimationFrame callbacks fall relative to the microtask queue?
- A test passes when you await a resolved promise twice and fails when you do it once. What does that tell you about the code under test?
- Why does a chain of zero-delay timeouts run more slowly than you would expect?
Related questions
- A promise rejects and nothing is awaiting it. What does Node do?hardAlso on promises and microtasks4 min
- A save button calls an API, the request fails, the error appears in the console - and the UI still shows 'Saved'. Walk me through what is actually happening in that promise chain.mediumAlso on promises and javascript4 min
- Walk me through the phases of the Node event loop.mediumAlso on event-loop and microtasks4 min
- How is `this` determined in JavaScript, and why does a method lose it when you pass it as a callback?mediumAlso on javascript4 min