Event Loop
IntermediateWhy setTimeout(…, 0) still runs after the rest of your code, and where promises fit in.Builds on Synchronous vs Asynchronous
Empty
Empty
Empty
Empty
- Running code
- Running microtasks
- Taking the next task
- Waiting
Empty
Spaceplay or pause←→previous or next stepRback to the start
Explanation
2 min read
On this page
What is the event loop in JavaScript?
JavaScript runs on a single thread: one call stack, one thing at a time. Slow work such as timers, network requests and user events is handed to the environment (the browser's Web APIs, or Node.js). When that work finishes, its callback waits in a queue. The decides when each waiting callback gets to run.
How does the event loop decide what runs next?
Callbacks wait in one of two queues:
- The microtask queue: promise callbacks (
then,catch,finally) and the code after anawait. - The task queue, also called the macrotask or callback queue: timers, events and I/O.
The loop repeats one rule:
- Run the current task until the call stack is empty.
- Run every microtask, including the ones queued along the way.
- Take the next task from the task queue and start again.
Microtasks always run before the next task. That is why a resolved promise beats
setTimeout(…, 0).
What mistakes do people make with the event loop?
Mini example
setTimeout(() => console.log('task'), 0);Promise.resolve().then(() => console.log('microtask'));console.log('sync'); // sync// microtask// taskQuick check
In which order does the mini example log its lines?
Further reading
Watch the video
The full explanation from the YouTurkey Tech YouTube channel, in Arabic.
All steps as text
- Step 1:
The whole script runs as one task. It goes onto the call stack and runs synchronously, top to bottom.
- Step 2:
console.log('start')runs immediately and prints start.Line 1
- Step 3:
setTimeoutis called, so it goes on top of the call stack.Lines 3–5
- Step 4:
setTimeout hands the callback to the browser's timer, a Web API, and returns at once. The script does not wait.
Lines 3–5
- Step 5:
The 0 ms timer is done, so the callback moves to the task queue. It must wait there until the call stack is empty.
Lines 3–5
- Step 6:
Promise.resolve()gives an already resolved promise, and.thenis called on it.Line 7
- Step 7:
Because the promise is already resolved,
.thenqueues its callback as a microtask right away.Lines 7–9
- Step 8:
console.log('end')prints end. Both callbacks are still waiting.Line 11
- Step 9:
The script task is done and the call stack is empty. Before anything else, the event loop empties the microtask queue.
- Step 10:
The promise callback moves onto the call stack.
Lines 7–9
- Step 11:
It prints promise.
Line 8
- Step 12:
The callback returns. The microtask queue is now empty.
Line 9
- Step 13:
Only now does the event loop take the next task from the task queue.
- Step 14:
The timeout callback finally moves onto the call stack.
Lines 3–5
- Step 15:
It prints timeout.
Line 4
- Step 16:
The callback returns. With the stack and both queues empty, the event loop waits for new work.
Line 5
- Step 17:
The output is start, end, promise, timeout: synchronous code first, then microtasks, then tasks.