Understand JavaScript promise and timer order
By Midhul · Updated 18 November 2026
When a promise logs before a zero-delay timer, trace the order of callbacks. Predict the output of each example below, then paste it into the Event Loop lab and step through the captured events.
Open the Event Loop visualizerPredict the order of synchronous code, a promise and a timer
console.log('start');
setTimeout(() => console.log('timer'), 0);
Promise.resolve().then(() => console.log('promise'));
console.log('end');Expected console order
start → end → promise → timer
The script finishes its synchronous work first. The resolved promise queues a reaction for the microtask checkpoint. The timer callback runs in a later task. A zero-millisecond delay does not interrupt the current script.
Follow a promise created inside a timer
console.log('start');
setTimeout(() => {
console.log('timer A');
Promise.resolve().then(() => console.log('promise inside A'));
}, 0);
setTimeout(() => console.log('timer B'), 0);Expected console order
start → timer A → promise inside A → timer B
The two timers are registered in order. After the first timer callback finishes, its promise reaction runs at the microtask checkpoint before the next timer task. Microtasks can therefore appear between two timer callbacks.
Recover from a rejected promise
Promise.reject('failed')
.catch(reason => console.log('recovered', reason))
.then(() => console.log('continued'));
console.log('sync');Expected console order
sync → recovered failed → continued
The catch callback handles the rejection and returns normally. The next promise in the chain can then fulfill, allowing the final callback to run. Throwing inside catch would instead keep the chain rejected until another handler recovers.
Read the visualization carefully
The lab captures console logs, timer callbacks and explicit promise callbacks. Its reaction panel includes registered callbacks waiting for promises to settle. Registration alone does not make a callback ready to execute. Use the captured console order to check your prediction.
The runner stops after five seconds and limits traces to 500 events. DOM access and network requests are unavailable. Await continuations, queueMicrotask, intervals and timer cancellation are outside the traced examples. Use short examples with explicit promise callbacks.
Try one change at a time
Move the resolved promise into the first timer, add another log after the timer registrations, or return a value from catch and log it in the next callback. Write down your prediction before running the modified code.
For the browser scheduling model, read the MDN explanation of tasks and microtasks. MDN microtask guide