First Principles
Programming

The Event Loop in JavaScript

The event loop keeps single-threaded JavaScript non-blocking by offloading slow work to the Web APIs, draining microtasks before each macrotask.

By Fadjar Rafi. Published 20 Jun 2026. 3 min read.

Contents
  1. What is the event loop in JavaScript?
  2. Web API operations that take a while
  3. How the event loop works, step by step

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.

How the event loop works: delegate to Web APIs, stay non-blocking, drain microtasks before the next macrotask, then repeat

Web API operations that take a while#

Some browser operations are slow enough that you don't want to sit and wait for them:

  1. 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.
  2. FileReader — reading a large file a user uploaded, like fileReader.readAsDataURL(largeImageFile).
  3. IndexedDB — reading or writing a lot of data, like indexedDB.getAll() on a store with thousands of entries.
  4. Media processing — decoding and playing large audio or video, like video.play() on a 4K clip.
  5. 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:

  1. Synchronous tasks run straight on the Call Stack.
  2. Microtasks are high-priority async work, like Promise callbacks.
  3. 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:

  1. Run all synchronous code on the Call Stack.
  2. Drain the entire Microtask Queue.
  3. Take one task from the Callback Queue.
  4. 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:

  1. Sync code runs first. A function gets pushed onto the Call Stack, executes, and pops off, one at a time.
  2. 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.
  3. 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.
  4. 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.
  5. Repeat. The cycle keeps turning as long as there's work left, which is how JavaScript stays asynchronous without ever blocking the main thread.