CodePeek · Free JavaScript classroom activity · 1 / 3
Promise vs setTimeout
TEACHER KEY · Tested output and queue reasoning
Keep this separate until learners have made their predictions. These are the same three snippets as the learner sheet and live tool.
Treat each snippet as an independent, isolated browser script. Every Promise.resolve() here is already fulfilled and each snippet has one timer. Predict output order, not elapsed time: a requested 0 ms delay is not an exact execution time. This activity does not model unrelated browser work or every event-loop phase.
1. A Promise and a zero-delay timer
console.log("start");
setTimeout(() => {
console.log("timer");
}, 0);
Promise.resolve().then(() => {
console.log("promise");
});
console.log("end");Tested output
start
end
promise
timerQueue reasoning · in execution order
- setTimeout registers a timer with a requested delay of 0 ms. It does not execute the callback now; 0 is not an exact execution time.
- This Promise is already fulfilled. Calling then queues its callback as a microtask; the callback has not run yet.
- The synchronous script finishes. At the microtask checkpoint, queued microtasks are processed before another task can run.
- Take promise from the front of the microtask queue and execute its callback. Scheduling and execution are different steps.
- The Promise callback returns. The microtask queue is empty; the timer callback can run in a later task when eligible.
- The browser now runs the eligible timer callback as a task. This is an ordering view, not a clock or a guarantee of elapsed time.
- The timer callback returns. All work from this example is complete.
Takeaway: Registration order is not execution order: the script finishes, the queued Promise callback runs, then the timer callback runs.
CodePeek · Free JavaScript classroom activity · 2 / 3
Promise vs setTimeout
TEACHER KEY · Tested output and queue reasoning
Keep this separate until learners have made their predictions. These are the same three snippets as the learner sheet and live tool.
Treat each snippet as an independent, isolated browser script. Every Promise.resolve() here is already fulfilled and each snippet has one timer. Predict output order, not elapsed time: a requested 0 ms delay is not an exact execution time. This activity does not model unrelated browser work or every event-loop phase.
2. A microtask queues another microtask
setTimeout(() => {
console.log("timer");
}, 0);
Promise.resolve().then(() => {
console.log("first");
Promise.resolve().then(() => {
console.log("nested");
});
});
Promise.resolve().then(() => {
console.log("second");
});
console.log("sync");Tested output
sync
first
second
nested
timerQueue reasoning · in execution order
- Register the timer. Its callback waits for a later eligible task; it does not interrupt the script.
- Queue first because then is called on an already fulfilled Promise.
- Queue second behind first. Microtasks in this example are processed in queue order.
- The script finishes. Begin draining the microtask queue.
- Remove first from the front and begin its callback. Second is still queued.
- While first is running, queue nested at the back, after second. Nested does not execute inside this call to then.
- First returns. Keep draining microtasks, including any newly queued during this checkpoint.
- Second was queued before nested, so second runs next.
- Second returns. Nested is still waiting in the microtask queue.
- Run nested before moving on to a timer task. The checkpoint does not stop at the original set of microtasks.
- The microtask queue is now empty. The eligible timer can run in a later task.
- Begin the timer callback, after the microtask checkpoint.
- The timer callback returns. This example is complete.
Takeaway: Microtasks queued during a microtask checkpoint also run before the next task, behind microtasks already waiting.
CodePeek · Free JavaScript classroom activity · 3 / 3
Promise vs setTimeout
TEACHER KEY · Tested output and queue reasoning
Keep this separate until learners have made their predictions. These are the same three snippets as the learner sheet and live tool.
Treat each snippet as an independent, isolated browser script. Every Promise.resolve() here is already fulfilled and each snippet has one timer. Predict output order, not elapsed time: a requested 0 ms delay is not an exact execution time. This activity does not model unrelated browser work or every event-loop phase.
3. A Promise created inside a timer
setTimeout(() => {
console.log("timer");
Promise.resolve().then(() => {
console.log("inside");
});
console.log("timer end");
}, 0);
Promise.resolve().then(() => {
console.log("promise");
});
console.log("sync");Tested output
sync
promise
timer
timer end
insideQueue reasoning · in execution order
- Register the timer, without executing its body. Inside is not queued yet: that call to then is inside a callback that has not run.
- Queue the outer Promise callback as a microtask.
- The script finishes; process queued microtasks before the next task.
- Run the outer Promise callback. Inside is still not queued.
- The outer callback returns, leaving the microtask queue empty.
- Begin the eligible timer task. Its body runs synchronously until it returns.
- Only now is inside queued. A microtask does not interrupt the currently executing timer callback.
- The timer callback returns. At the next microtask checkpoint, process inside.
- Take inside from the queue and run its callback.
- The inner callback returns. This example is complete.
Takeaway: A Promise callback can only run after it is queued. A microtask created inside a timer waits until that timer callback finishes.