Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

Why does a Promise run before setTimeout 0?

Do you know what will be the output of this code?

JavaScript
console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");
Show answer and explanation

The answer is A D C B.

Synchronous code runs first: A, D. Then the event loop empties the microtask queue (promise callbacks): C. Only then does it run the next task (the timer): B.

Take the full quiz Next question →

More Promises & Async questions

  1. Why does an arrow function in setTimeout keep this?
  2. What does an async function return in JavaScript?
  3. What runs first after await in JavaScript?
  4. Is the Promise constructor callback synchronous?
  5. What happens when one promise in Promise.all rejects?

Related topics