Skip to the solution
EasyFrontend6 min read

What is the difference between synchronous and asynchronous code?

420 views

The problem

Question presented to candidate: "Walk me through, in exact order, what actually prints if a function has a console.log, then a setTimeout(fn, 0), then a Promise.then(fn), then another console.log β€” and explain why that order happens."

What a strong answer should cover:

  • πŸ“Œ Interview term: synchronous code β€” executes one statement at a time, top to bottom, each statement genuinely blocking the next from starting until it completes.
  • πŸ“Œ Interview term: asynchronous code β€” lets long-running or externally-triggered work (a timer, a network request, a file read) happen without blocking the rest of the program; the result is delivered later via a callback, a Promise, or async/await.
  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” verified directly: given a synchronous log, a setTimeout(fn, 0), a Promise.then(fn), and a second synchronous log, the real observed order was all synchronous code first (both logs), then the Promise callback, then the setTimeout callback β€” never interleaved any other way.
  • πŸ“Œ Interview term: the microtask/macrotask distinction β€” a precise answer names WHY the Promise callback ran before the setTimeout callback despite both being "asynchronous": Promise callbacks are microtasks, which the event loop always fully drains before running the next macrotask (which is what a setTimeout callback is), even a setTimeout with a 0ms delay.
  • A precise answer names that "asynchronous" does not mean "runs on a separate thread" β€” JavaScript itself is single-threaded; asynchronous operations are handled by the surrounding runtime (browser APIs or Node's libuv), which schedules their callbacks back onto the same single JS thread once ready.

Clarifying questions expected:

  • None β€” this is a definitional/comparison question; producing the exact real ordering (not just "sync runs first, async runs later") is the strong signal.

Code / implementation expected: Yes β€” reproducing the exact real console.log order for the prompt's own scenario is the clearest, most convincing demonstration.

  • sync-async

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof of the exact synchronous-then-microtask-then-macrotask execution order

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 28 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

Why would you use performance.now() instead of Date.now() to measure how long a function takes to run?

Continue