Fetching data from an API with proper error handling
The Fetch API provides a modern way to make HTTP requests in the browser.
It returns a Promise that resolves to a Response object, which you can then
parse to extract the data.
This example demonstrates the fundamental pattern for making GET requests:
calling fetch(), checking the response status, and parsing the response body.
Basic Pattern
// Simple GET request with async/awaitasync functionfetchData(url) {
const response = awaitfetch(url);
// Check if the response was successfulif (!response.ok) {
throw newError(`HTTP error! Status: ${response.status}`);
}
// Parse JSON response bodyconst data = await response.json();
return data;
}
// Usagetry {
const users = awaitfetchData('https://api.example.com/users');
console.log(users);
} catch (error) {
console.error('Failed to fetch:', error.message);
}
Key concepts:
fetch() returns a Promise that resolves when headers are received (not when the body is complete)
response.ok is true for status codes 200-299
response.json() returns a Promise that parses the body as JSON
Network errors (no connection, DNS failure) reject the Promise
HTTP errors (404, 500) do NOT reject — you must check response.ok
The second argument to fetch() is an options object where you can specify
headers, method, body, credentials, and more. The Accept header tells the
server what content types you can handle.
Both patterns are equivalent. Use async/await for cleaner sequential code,
or .then() chains when you need to integrate with existing Promise-based code.
Inspecting the Response
const response = awaitfetch(url);
// Response metadata
console.log(response.status); // 200
console.log(response.statusText); // "OK"
console.log(response.ok); // true (status 200-299)
console.log(response.url); // Final URL (after redirects)
console.log(response.redirected); // true if redirected// Response headers
console.log(response.headers.get('Content-Type'));
console.log(response.headers.get('Content-Length'));
// Parse body (can only be read once!)const json = await response.json(); // Parse as JSON// ORconst text = await response.text(); // Parse as text// ORconst blob = await response.blob(); // Binary data
Important
The response body can only be read once. Clone the response if you need to read it multiple times.
Always check response.ok before parsing — a 404 response still returns a Response object.
Use response.headers.get() to access response headers (some are restricted by CORS).