Call stack
Currently running synchronous JavaScript.
The event loop coordinates synchronous code, queued tasks and promise microtasks.
console.log("A");
setTimeout(() => console.log("C"), 0);
Promise.resolve().then(() => console.log("B"));
Async JavaScript
JavaScript runs synchronous code first. While that code is running, no other JavaScript can interrupt it.
Timers and events queue tasks. Promise callbacks queue microtasks, which run after the current synchronous code and before the next task.
This ordering explains why setTimeout with 0 milliseconds still runs after current code and after promise microtasks.
Currently running synchronous JavaScript.
Timers, events and other task callbacks.
Promise callbacks and queueMicrotask.
The browser can update the screen between work.
Examples
console.log("sync");
Promise.resolve().then(() => console.log("microtask"));
setTimeout(() => console.log("task"), 0);
setTimeout(runNow, 0); // It runs later, after current code and microtasks.
Code patterns
These examples are the async patterns you will reuse: timers, promises, composition, await, fetch and cancellation.
Runs immediately.
console.log("first");
console.log("second");
Runs in a future task.
setTimeout(() => console.log("timer"), 0);
Runs before the next timer task.
Promise.resolve().then(() => console.log("microtask"));
Queue a microtask explicitly.
queueMicrotask(() => updateAfterCurrentCode());
Rules that matter
Async JavaScript is easier when every operation has clear pending, success, failure and cancellation behavior.
The current stack must finish.
Promise callbacks have priority over timers.
They wait for the stack and browser scheduling.
Heavy synchronous work freezes the page.
Give the browser chances to respond.
Make ordering explicit with awaited promises.
Production thinking
The event loop explains async ordering bugs that otherwise feel random.
Long synchronous tasks can block keyboard input, focus changes and screen-reader updates.
Production performance work often starts by finding long tasks that block the event loop.
Client-rendered pages need JavaScript to complete reliably; event-loop blocking delays rendering.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
If you can explain when the code runs, how it fails and what happens if it becomes irrelevant, you understand the async model.
Senior audit upgrade
Async JavaScript feels clearer when you separate the call stack, microtasks, rendering and later tasks.
call stack -> microtasks (promises) -> browser may render -> tasks (timers, events) -> repeat