Skip to solution
hardFrontend

In what order do console.log, setTimeout, and Promise.then run?

775 views
01

Understand the problem

Question presented to candidate: "If I mix several console.log calls with a couple of setTimeout(fn, 0) calls and a couple of Promise.then calls, in what exact order do they all print, and why?"

What a strong answer should cover:

  • Every synchronous console.log runs first, in the exact order it appears in the source -- before any queued callback runs at all.
  • After the synchronous code finishes, the ENTIRE microtask queue drains -- every Promise.then callback runs, in the order those callbacks were actually scheduled, not the order the .then() calls appear if scheduling happens indirectly.
  • setTimeout callbacks run only after the whole microtask queue reports empty, and among themselves, they run in the order they were scheduled.
  • A chained .then() (a second .then() attached to the result of the first) is scheduled ONLY once the first .then() callback actually runs -- so a two-deep chain takes two full microtask-queue passes, not one.
  • This is not a memorized fact -- it follows directly from two queues (microtask, macrotask) and one rule: the microtask queue always fully drains before the next macrotask runs.

Clarifying questions expected:

  • "Should I also cover process.nextTick, or keep this to the standard browser-style microtask/macrotask model?"

Code / implementation expected: Yes -- a short, runnable snippet mixing synchronous logs, a chained .then(), a separate .then(), and two setTimeout calls, with the real observed order.

event-looporder
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for the single most common JavaScript async-ordering interview question. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every result below was actually run on Node.js v24.19.0

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSMixed sync logs, chained then, separate then, two setTimeouts (run directly)
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 135 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track