Skip to solution
easyFrontend

What is the difference between synchronous and asynchronous code?

420 views
01

Understand 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
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 JavaScript fundamentals/event-loop interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. The exact ordering below was actually run in Node — not recited from a diagram

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal proof of the exact synchronous-then-microtask-then-macrotask execution order
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 28 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track