Course contentsShow
← JavaScript
Lesson 1492 of 2,72332. JavaScript in the BrowserFree lesson

Asynchronous Browser APIs

How browser APIs like fetch, setTimeout, and requestAnimationFrame integrate with the event loop.

Asynchronous Browser APIs

You'll learn how browser APIs like fetch, setTimeout, and requestAnimationFrame work with the event loop to handle tasks without blocking your code.


What Makes Browser APIs Asynchronous?

The browser provides special APIs that perform tasks over time—like waiting, fetching data, or scheduling animations. These operations would freeze your entire page if they ran synchronously. Instead, they work asynchronously, delegating the actual work to the browser's internals while your JavaScript continues running.

When you call setTimeout, the browser (not JavaScript) manages the timer. When the delay completes, the browser places your callback in the task queue. The event loop then picks it up when the call stack is empty.

How They Integrate with the Event Loop

Think of the event loop as a receptionist managing different waiting rooms:

  • setTimeout and setInterval callbacks go to the task queue (also called macrotask queue)
  • fetch promises resolve and their .then() handlers go to the microtask queue (higher priority)
  • requestAnimationFrame callbacks wait in a special animation queue, fired before the next repaint
console.log('Start');

setTimeout(() => console.log('Task queue'), 0);

fetch('/api/data')
  .then(() => console.log('Microtask queue'));

requestAnimationFrame(() => console.log('Animation frame'));

console.log('End');

// Output order:
// Start → End → Microtask queue → Animation frame → Task queue

The browser handles the work (network request, timer countdown, frame scheduling), then notifies JavaScript via the event loop when results are ready.

Key Takeaway: Asynchronous browser APIs offload work to the browser's internal systems and communicate results back through the event loop's queuing mechanisms, keeping JavaScript non-blocking and responsive.