FREE JAVASCRIPT EVENT-LOOP TOOL
Promise vs setTimeout.
Who logs first?
Predict a short snippet, then watch what runs now and what waits. Three small examples. One clearer mental model.
Try the prediction tool ↓2. Predict the output. 3. Check it and step through.
Predict the console output
Read from top to bottom. Each log prints on its own line.
console.log("start");
setTimeout(() => {
console.log("timer");
}, 0);
Promise.resolve().then(() => {
console.log("promise");
});
console.log("end");
Step through the event loop
Your turn first! Choose an output and check your prediction to unlock the queues and execution trace.
Free, no signup. Three fixed, tested teaching examples—not a code runner. Predictions stay in page memory and clear on reload. No submitted code or saved results. The trace models an isolated browser script with fulfilled Promises and one timer, not every event-loop phase or unrelated browser work.
TEACH WITH THESE THREE SNIPPETS
Bring the queue into class.
Ask learners to predict independently, compare their reasoning, then trace together. Use an answer-free learner sheet, a separate public teacher explanation key, or the guest embed. No signup needed.
THE ORDER, NOT A STOPWATCH
Why does a Promise callback run before setTimeout?
In a browser script that calls Promise.resolve().then(callback) and setTimeout(callback, 0), both callbacks are scheduled while the synchronous script continues. Neither callback interrupts the running script.
- Run synchronous JavaScript. Calling
thenon an already fulfilled Promise queues its callback as a microtask. CallingsetTimeoutregisters a timer whose callback will run as a later task. - Drain microtasks at the checkpoint. After the current synchronous work finishes, queued microtasks run in order. Newly queued microtasks also run at this checkpoint, behind those already waiting.
- Run an eligible timer task later. After the checkpoint, the browser can execute an eligible timer callback. Microtasks queued inside that callback wait until it finishes.
Does setTimeout(..., 0) mean “right now”?
No. The delay is a requested waiting interval, not an exact execution time. Current work, microtasks, browser scheduling and timer clamping can delay execution. This visualizer shows callback order, not milliseconds.
Do Promises always beat timers?
No. A Promise callback must first be queued. A pending Promise’s then handler waits for that Promise to settle; it is not necessarily ready before a timer. In these examples, every Promise.resolve() is already fulfilled. The third example shows why a Promise created inside a timer cannot run before that timer starts.
Read more: MDN Promise timing, MDN microtask guide and MDN timer delays.
KEEP THE AHA GOING
Try a free async puzzle
Put the same model to work in the existing free starter set. Play as a guest; optional accounts save practice across devices.
