SyntaxStudy
Sign Up
JavaScript Beginner 3 min read

The Fetch API

Fetch API

The Fetch API provides a modern, promise-based interface for making HTTP requests. It replaces XMLHttpRequest with a cleaner, more composable API.

Example
const res = await fetch("https://api.example.com/users");
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const users = await res.json();
// Check status and content-type before parsing
console.log(res.status, res.headers.get("content-type"));
Pro Tip

fetch() only rejects on network error — a 404 or 500 still resolves. Always check res.ok.