whatstheoutput_

#6: Promise vs setTimeout ordering

Topic: async · Difficulty: ★★

console.log('a');
setTimeout(() => console.log('b'));
Promise.resolve().then(() => console.log('c'));
console.log('d');

Output

a
d
c
b

Why

Synchronous code always runs to completion first: 'a', then 'd'. The Promise callback goes into the microtask queue and the setTimeout callback into the macrotask (timer) queue. After the script finishes, the event loop drains ALL microtasks before it takes the next macrotask — so the promise's 'c' prints before the timeout's 'b', even though the setTimeout was registered first. Rule of thumb: sync code, then promises, then timers.

Play today's puzzle →