Chapter XVI · Asynchronous JavaScript

Web APIs

Web APIs are built-in browser features (like setTimeout, fetch, DOM events) that handle asynchronous tasks outside the core JavaScript engine.

01 How It Works

Think of department store customer service counters handling specialized tasks while the main cashier deals with checkout.

When you call an async Web API, the browser runs the background operation independently from the main JavaScript execution thread.

example.js
window.setTimeout(() => {
  console.log("Timer finished in Web API environment");
}, 2000);
  1. 1. JavaScript calls a Web API method (e.g., fetch or timer).

  2. 2. Browser Web API handles the background operation.

  3. 3. Upon completion, the callback is pushed into the task queue.

02 Practical Example

Here is how you might see this concept applied in real-world code:

practical.js
navigator.geolocation.getCurrentPosition(position => {
  console.log(position.coords.latitude);
});

Key Takeaway

Web APIs provide the underlying machinery for browser-based asynchronous operations.

Related Concepts