Promise chain vs queued microtasks and timer
Assume a single browser task. Write each log in order.
Code Snippet
console.log("start");
setTimeout(() => console.log("timer"), 0);
const ready = new Promise(resolve => {
console.log("executor");
resolve();
});
ready.then(() => {
console.log("then 1");
queueMicrotask(() => console.log("inner"));
}).then(() => console.log("then 2"));
queueMicrotask(() => console.log("outer"));
console.log("end");Expected Output & Explanation
start
executor
end
then 1
outer
inner
then 2
timer The executor runs inline. then 1 queues inner before the chained then 2 becomes eligible, but outer is already waiting ahead of both. The timer runs after the microtask queue drains.
Module Context & References
JavaScript · Async output drills Trace microtasks, promise identity, thenables and error recovery
Personal Notes · Markdown