Skip to content
BENIBIX Learn

Fetch API

Development🌐 Web DevelopmentBrowser Code

Also known as fetch(), JavaScript Fetch, window.fetch

The way code in a web page asks a server for data.

Think of it like

Think of it like sending a friend to the shop. You keep playing, and they come back with the bag.

Example

Lucy's weather page calls fetch("/api/weather"). Soon it shows 18°C, and the page does not reload.

Why it matters

Most modern pages load data this way after they open. It is why a feed can update without a full reload.

Definition

The Fetch API is the browser's built-in way to ask a server for data from JavaScript. You call fetch with a web address. It sends the request and hands back a promise. The page keeps working while it waits. When the answer comes, you read it, often as JSON. Node.js has fetch built in too.

Example code

javascript
const res = await fetch("/api/weather");
if (!res.ok) throw new Error(res.status);
const data = await res.json();
Ask for the data, check it worked, then read the JSON.
The page sends a request, keeps working, and fills in the answer when it comes.

Common mistake

Thinking fetch fails on a 404 or 500. It does not. You have to check res.ok yourself.

Key takeaways

  1. 1
    Built in. No library is needed in the browser.
  2. 2
    Works with promises. The page keeps running while it waits.
  3. 3
    Check the status. An error page still counts as an answer.

Learn it properly