Skip to content
JS Trap

JS Trap

JavaScript Event Loop: Why a Promise Runs Before setTimeout 0

· JavaScript event loop: setTimeout 0 vs Promise | kaun pehle chalega?

Predict the output: log A, a 0 ms setTimeout logs B, Promise.then logs C, log D. Node prints A D C B. Sync code runs first, then the event loop drains every microtask (promises) before the next task (timers). A classic JavaScript interview question.

The code

code
console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");
Daily TrapOne more trap, every dayA short JavaScript or Python output puzzle each day. Guess, see why, keep your streak.