Fetch API: Making Basic Network Requests

The Fetch API is like ordering from a catalog: you give it a URL and get a promise of delivery. It's used to load data from APIs without a page reload. The footgun: the promise resolves even on HTTP errors (like 404); you must check.
Why it exists
Web applications need to communicate with servers without reloading the page. The original method, XMLHttpRequest, was complex and callback-based. The Fetch API was created to provide a modern, promise-based, and more flexible interface for network requests.
The mental model
Think of fetch() as a two-step delivery process. First, you call fetch(url). This immediately returns a Promise that represents the attempt to make the request. This promise resolves into a Response object as soon as the server sends back its headers. This Response is just the "shipping notification," not the package itself. The second step is to unpack the package by calling a method on the response, like response.json(), which also returns a promise that resolves with the actual body content.
How it works
The fetch() method takes a URL as its first argument. It returns a Promise that resolves to a Response object. This object contains metadata like the status code (response.status) and headers. The body of the response is a stream and is not immediately available. To access it, you must call a parsing method like response.json() for JSON data or response.text() for plain text. Each of these methods returns another promise that resolves with the parsed body content.
When to use it
Use fetch() for nearly all modern client-side network requests. It's ideal for getting data from REST or GraphQL APIs, submitting data via POST requests (by providing an options object), and loading any resource asynchronously. It's available in the main browser window and in Web Workers, making it universally accessible.
When not to use it
The biggest footgun is that fetch() only rejects its promise on a network failure (like no internet connection), not on HTTP error statuses (like 404 Not Found or 500 Internal Server Error). You must always check the response.ok property to handle these cases. For cross-origin requests that need cookies, you must explicitly set the credentials: 'include' option. For very old browsers, you may need a polyfill.
One canonical example
To get user data from a JSON API, you first fetch the URL, then check if the response was successful, and only then parse the JSON body. A common pattern using async/await looks like this:
async function getUser() {
try {
const response = await fetch('https://api.example.com/users/1');
if (!response.ok) {
throw new Error(HTTP error! Status: ${response.status});
}
const user = await response.json();
console.log(user.name);
} catch (error) {
console.error('Fetch error:', error);
}
}This example correctly checks response.ok before trying to use the data.
Interview question
What condition will cause the Promise returned directly by a fetch() call to reject?
- a.The response.ok property evaluates to false.
- b.The response.json() method fails to parse the body content.
- c.The server responds with an HTTP status code indicating an error (e.g., 404 or 500).
- d.A network error occurs, preventing the request from being sent or received.Correct
Why? this is the answer
The card explicitly states that "fetch() only rejects its promise on a network failure (like no internet connection)". Options A and D describe scenarios where the promise resolves, but with an HTTP error status, which must be checked using response.ok. Option B refers to a rejection of the *second* promise, returned by methods like response.json(), not the initial fetch() promise.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on web apis — each one lists the topics its interview covers.
See open roles