Guides/Microtask vs Macrotask: 10 Output Puzzles

Microtask vs Macrotask: 10 Output Puzzles

Predict the console order, then open each snippet in the visualizer and step through queues.

Rajan Pandey · About the author

Rajan Pandey is a Full Stack AI Engineer with nearly 3 years of professional experience building production-ready web applications, SaaS platforms, and developer-focused tools. He specializes in JavaScript, TypeScript, React.js, Next.js, Node.js, and the MERN stack.He has worked on 15+ production applications across SaaS, EdTech, and GenAI products, with experience spanning frontend architecture, backend development, REST APIs, database design, authentication, and third-party integrations. StackTools was created as a way to make everyday development tasks simpler and JavaScript concepts easier to understand through interactive, browser- based tools.His goal is to build practical utilities that developers can use without unnecessary complexity, registration, or uploading their data to external servers.

How to use these puzzles

Cover the answer, predict the order, then click Open in Visualizer and press Run. If your prediction differs, scrub the microtask queue and timer panel until the mismatch clicks. These assume a normal browser/Node main-thread model — not Worker threads.

Puzzle 1 — Baseline

console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");
Open in Visualizer

Answer: A D C B

Sync A/D, then microtask C, then macrotask B.

Puzzle 2 — Two timers

setTimeout(() => console.log("t1"), 0);
setTimeout(() => console.log("t2"), 0);
Promise.resolve().then(() => console.log("p"));
console.log("s");
Open in Visualizer

Answer: s p t1 t2

One sync log, one microtask, then macrotasks in schedule order.

Puzzle 3 — then chains

Promise.resolve()
  .then(() => console.log(1))
  .then(() => console.log(2));
Promise.resolve().then(() => console.log(3));
console.log(0);
Open in Visualizer

Answer: 0 1 3 2

Both first reactions are queued before either runs; 2 is queued after 1 finishes.

Puzzle 4 — queueMicrotask

queueMicrotask(() => console.log("m"));
setTimeout(() => console.log("t"), 0);
console.log("s");
Open in Visualizer

Answer: s m t

queueMicrotask is explicitly a microtask.

Puzzle 5 — Promise in timer

setTimeout(() => {
  console.log("t");
  Promise.resolve().then(() => console.log("p"));
}, 0);
console.log("s");
Open in Visualizer

Answer: s t p

After macrotask t starts, its microtask p runs before the next macrotask.

Puzzle 6 — Nested then

Promise.resolve().then(() => {
  console.log("A");
  Promise.resolve().then(() => console.log("B"));
});
Promise.resolve().then(() => console.log("C"));
Open in Visualizer

Answer: A C B

A and C are first-wave microtasks; B is queued while A runs and runs after C.

Puzzle 7 — async function

async function f() {
  console.log("1");
  await Promise.resolve();
  console.log("3");
}
f();
console.log("2");
Open in Visualizer

Answer: 1 2 3

Body until await is sync; resumption after await is a microtask.

Puzzle 8 — reject path

Promise.reject("x").catch(() => console.log("c"));
Promise.resolve().then(() => console.log("t"));
console.log("s");
Open in Visualizer

Answer: s c t

catch reactions are also microtasks; order follows queue order among first reactions.

Puzzle 9 — Zero vs one ms

setTimeout(() => console.log("0"), 0);
setTimeout(() => console.log("1"), 1);
Promise.resolve().then(() => console.log("p"));
Open in Visualizer

Answer: p 0 1

Microtask first; both timers are macrotasks, 0ms typically before 1ms.

Puzzle 10 — Microtask storm before paint

setTimeout(() => console.log("macrotask"), 0);
function storm(n) {
  if (n <= 0) return;
  queueMicrotask(() => {
    console.log("m" + n);
    storm(n - 1);
  });
}
storm(3);
console.log("sync");
Open in Visualizer

Answer: sync m3 m2 m1 macrotask

Each microtask schedules another; the drain finishes before the timer.

When not to overfit puzzles

Interview puzzles compress the event loop into printable logs. Real apps also contend with rendering, input, network, and prioritization. Use these to lock the microtask-before-timer rule, then profile actual jank with Performance tools — not more console gymnastics.

Try it live

Open the matching StackTools utility — runs in your browser.

Open JS Execution Visualizer