Repaso
Español
Frontend & WebAdvancedFE-04

In what order are the letters logged?

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

Options

  1. A B C D
  2. A D C B (Correct option)
  3. A D B C
  4. A C D B

Answer

A D C B

Why

JavaScript runs on one thread with an event loop:

  1. First all normal (synchronous) code runs: A, then D.
  2. Then the microtask queue is emptied: promise callbacks → C.
  3. Then the next macrotask runs: timers like setTimeout → B.

So promise callbacks always run before timers, even timers set to 0 ms.

Remember

Sync code → promise callbacks (microtasks) → timers (macrotasks).

Key terms

Practice this question in Repaso