Fetch API
The Fetch API provides a modern, promise-based interface for making HTTP requests. It replaces XMLHttpRequest with a cleaner, more composable API.
The Fetch API provides a modern, promise-based interface for making HTTP requests. It replaces XMLHttpRequest with a cleaner, more composable API.
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"));
fetch() only rejects on network error — a 404 or 500 still resolves. Always check res.ok.
More in JavaScript