console.log("A") console.log("B") console.log("C") CODE_BLOCK: console.log("A") console.log("B") console.log("C") CODE_BLOCK: console.log("A") console.log("B") console.log("C") CODE_BLOCK: A B C CODE_BLOCK: function main() { a() } function a() { b() } function b() { console.log("hello") } main() CODE_BLOCK: function main() { a() } function a() { b() } function b() { console.log("hello") } main() CODE_BLOCK: function main() { a() } function a() { b() } function b() { console.log("hello") } main() CODE_BLOCK: 1 main() 2 a() 3 b() 4 console.log() CODE_BLOCK: 1 main() 2 a() 3 b() 4 console.log() CODE_BLOCK: 1 main() 2 a() 3 b() 4 console.log() COMMAND_BLOCK: console.log("start") setTimeout(() => { console.log("timeout") }, 0) console.log("end") COMMAND_BLOCK: console.log("start") setTimeout(() => { console.log("timeout") }, 0) console.log("end") COMMAND_BLOCK: console.log("start") setTimeout(() => { console.log("timeout") }, 0) console.log("end") CODE_BLOCK: start end timeout CODE_BLOCK: start end timeout CODE_BLOCK: start end timeout COMMAND_BLOCK: console.log("A") setTimeout(() => { console.log("B") }, 0) console.log("C") COMMAND_BLOCK: console.log("A") setTimeout(() => { console.log("B") }, 0) console.log("C") COMMAND_BLOCK: console.log("A") setTimeout(() => { console.log("B") }, 0) console.log("C") CODE_BLOCK: A C B COMMAND_BLOCK: console.log("start") setTimeout(() => { console.log("timeout") }, 0) Promise.resolve().then(() => { console.log("promise") }) console.log("end") COMMAND_BLOCK: console.log("start") setTimeout(() => { console.log("timeout") }, 0) Promise.resolve().then(() => { console.log("promise") }) console.log("end") COMMAND_BLOCK: console.log("start") setTimeout(() => { console.log("timeout") }, 0) Promise.resolve().then(() => { console.log("promise") }) console.log("end") CODE_BLOCK: start end promise timeout CODE_BLOCK: start end promise timeout CODE_BLOCK: start end promise timeout CODE_BLOCK: async function test() { return 1 } CODE_BLOCK: async function test() { return 1 } CODE_BLOCK: async function test() { return 1 } CODE_BLOCK: Promise.resolve(1) CODE_BLOCK: Promise.resolve(1) CODE_BLOCK: Promise.resolve(1) CODE_BLOCK: async function main() { console.log("A") const value = await Promise.resolve(1) console.log("B") } main() console.log("C") CODE_BLOCK: async function main() { console.log("A") const value = await Promise.resolve(1) console.log("B") } main() console.log("C") CODE_BLOCK: async function main() { console.log("A") const value = await Promise.resolve(1) console.log("B") } main() console.log("C") CODE_BLOCK: A C B CODE_BLOCK: async function main() { const value = await getData() console.log(value) } CODE_BLOCK: async function main() { const value = await getData() console.log(value) } CODE_BLOCK: async function main() { const value = await getData() console.log(value) } COMMAND_BLOCK: getData().then(value => { console.log(value) }) COMMAND_BLOCK: getData().then(value => { console.log(value) }) COMMAND_BLOCK: getData().then(value => { console.log(value) }) CODE_BLOCK: async function main() { console.log("1") await Promise.resolve() console.log("2") } console.log("3") main() console.log("4") CODE_BLOCK: async function main() { console.log("1") await Promise.resolve() console.log("2") } console.log("3") main() console.log("4") CODE_BLOCK: async function main() { console.log("1") await Promise.resolve() console.log("2") } console.log("3") main() console.log("4") CODE_BLOCK: 3 1 4 2 CODE_BLOCK: 3 1 4 2 CODE_BLOCK: 3 1 4 2 CODE_BLOCK: console.log("A") async function test() { console.log("B") await Promise.resolve() console.log("C") } test() console.log("D") CODE_BLOCK: console.log("A") async function test() { console.log("B") await Promise.resolve() console.log("C") } test() console.log("D") CODE_BLOCK: console.log("A") async function test() { console.log("B") await Promise.resolve() console.log("C") } test() console.log("D") CODE_BLOCK: A B D C CODE_BLOCK: A B D C CODE_BLOCK: A B D C CODE_BLOCK: const a = await fetchA() const b = await fetchB() CODE_BLOCK: const a = await fetchA() const b = await fetchB() CODE_BLOCK: const a = await fetchA() const b = await fetchB() CODE_BLOCK: const [a, b] = await Promise.all([ fetchA(), fetchB() ]) CODE_BLOCK: const [a, b] = await Promise.all([ fetchA(), fetchB() ]) CODE_BLOCK: const [a, b] = await Promise.all([ fetchA(), fetchB() ]) CODE_BLOCK: const a = await fetchA() const b = await fetchB() CODE_BLOCK: const a = await fetchA() const b = await fetchB() CODE_BLOCK: const a = await fetchA() const b = await fetchB() CODE_BLOCK: const aPromise = fetchA() const bPromise = fetchB() const a = await aPromise const b = await bPromise CODE_BLOCK: const aPromise = fetchA() const bPromise = fetchB() const a = await aPromise const b = await bPromise CODE_BLOCK: const aPromise = fetchA() const bPromise = fetchB() const a = await aPromise const b = await bPromise
- Why does Promise.then() run before setTimeout?
- Why does await run “later” even though it looks synchronous?
- What is actually happening behind async / await?
- Microtask Queue - setTimeout is called
- It is handed off to Web APIs
- Timer starts outside the call stack
- JavaScript continues execution
- Run everything in the call stack
- When empty → take one task from the task queue
- Push it to the call stack - setTimeout registers callback - call stack becomes empty - event loop pushes task - setInterval
- Promise.then - queueMicrotask
- Run call stack
- Run ALL microtasks
- Run ONE task
- Run microtasks again - fetchA runs - fetchB runs
- Call Stack → current execution
- Web APIs → timers, network
- Task Queue → setTimeout
- Microtask Queue → Promise
- Event Loop → orchestrates everything
- Promise → runs in microtask queue - setTimeout → runs in task queue - microtasks always run first - await → splits code into microtasks
- Why Promise runs before setTimeout
- Why await runs “later”
- Why sequential vs parallel happens