InterviewBuddy
All interview questions

Why does a Promise run before setTimeout?

Intermediate

Answer

After the current synchronous work finishes, JavaScript drains the microtask queue before starting the next task. Promise reactions are microtasks, while a timer callback runs as a task when eligible.

Explanation

A zero-delay timer is not immediate. It waits for the current work and microtasks. Long microtask chains can delay other tasks.

Example

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