Skip to the solution
MediumFrontend8 min read

What does setTimeout with 0 delay actually do?

566 views

The problem

Question presented to candidate: "If I call setTimeout with a delay of 0, does the callback run immediately? What is actually happening under the hood?"

What a strong answer should cover:

  • setTimeout(fn, 0) does NOT run fn synchronously or immediately -- it schedules fn as a macrotask, which always waits at minimum for the rest of the current synchronous code to finish.
  • Before that macrotask can run, the entire microtask queue must be completely empty -- even microtasks that get added after the setTimeout call still run first.
  • "0ms" is a request, not a guarantee -- both browsers and Node.js apply a real minimum delay floor, and a negative or missing delay is clamped the same way a 0 is.
  • Browsers additionally clamp nested setTimeout chains (calling setTimeout from inside a setTimeout callback, 5+ levels deep) to a minimum of 4ms, per the HTML spec -- a rule Node.js does not implement the same way.
  • Practical use: setTimeout(fn, 0) is a common way to defer work to "the next macrotask turn," letting the browser repaint or letting other pending events process, especially for breaking up long synchronous work.

Clarifying questions expected:

  • "Are we talking about a single setTimeout(0) call, or a chain of nested ones -- the nesting case has an extra clamping rule in browsers."

Code / implementation expected: Yes -- a short, runnable snippet showing a setTimeout(0) genuinely waiting for pending microtasks before running.

  • settimeout

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

8 min read Β· 9 sections

Run the code

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

JavaScriptsetTimeout(0) genuinely waits for 10 pending microtasks first (run directly)

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 82 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 nextMedium

Explain prototypal inheritance.

Continue