What is the event loop in JavaScript?#
JavaScript is single-threaded. It runs one instruction at a time, in order, and only one thing can happen at any given moment.
That sounds limiting, but the event loop is what lets JavaScript deal with slow operations without freezing everything else. It runs them off to the side, so the instructions still waiting in line don't have to stop.

Web API operations that take a while#
Some browser operations are slow enough that you don't want to sit and wait for them:
- Fetch / XMLHttpRequest — a network request can take anywhere from a few hundred milliseconds to several seconds, depending on the connection and how much data comes back. Think
fetch('https://api.example.com/data')pulling down a large dataset. - FileReader — reading a large file a user uploaded, like
fileReader.readAsDataURL(largeImageFile). - IndexedDB — reading or writing a lot of data, like
indexedDB.getAll()on a store with thousands of entries. - Media processing — decoding and playing large audio or video, like
video.play()on a 4K clip. - Geolocation —
navigator.geolocation.getCurrentPosition()can be slow, especially on a device with a weak GPS signal.
If JavaScript ran these on the main thread, the whole app would stop and wait for each one to finish. So it doesn't. Slow work gets handed off to the Web APIs, which live outside the main thread.
Picture a fetch pulling down a 10MB dataset. Handle it inline and the UI freezes for the entire download — no scrolling, no clicking, nothing until the data lands. With the event loop, JavaScript kicks off the fetch, moves on to the next line, and the user keeps interacting. When the download finishes, its callback waits in a queue until the call stack is clear, then runs.
That's what "non-blocking" means: a slow operation never locks the main thread. Heavy work runs in the background while the app stays responsive.
The event loop is responsible for:
- orchestrating what runs and when
- collecting and processing events
- running the tasks waiting in the queues
It deals with three kinds of work:
- Synchronous tasks run straight on the Call Stack.
- Microtasks are high-priority async work, like Promise callbacks.
- Macrotasks are lower-priority async work, like
setTimeout.
And the pieces it moves work between:
- Call Stack
- Web APIs
- Callback Queue (macrotasks)
- Microtask Queue
- the Event Loop itself
How the event loop works, step by step#
The cycle boils down to this:
- Run all synchronous code on the Call Stack.
- Drain the entire Microtask Queue.
- Take one task from the Callback Queue.
- Repeat.
The priority in steps 2 and 3 is the part people miss: every microtask clears before a single macrotask runs. So a Promise.then scheduled after a setTimeout(…, 0) still runs first.
In more detail:
- Sync code runs first. A function gets pushed onto the Call Stack, executes, and pops off, one at a time.
- Async work gets delegated. When JavaScript hits something like
setTimeout,fetch, or an event listener, it hands the operation to the Web APIs so it runs off the stack. - The callback gets queued. Once the async operation finishes, its callback goes into a queue — Promise callbacks to the Microtask Queue, everything else to the Callback Queue.
- The loop drains the queues. Whenever the Call Stack is empty, the event loop clears every microtask first, then pulls a single macrotask, and moves it onto the stack to run.
- Repeat. The cycle keeps turning as long as there's work left, which is how JavaScript stays asynchronous without ever blocking the main thread.