Our Main Messenger
The fetch() function is the modern way to make HTTP requests. By default it performs a GET request.
async function loadUsers() {
const response = await fetch('https://jsonplaceholder.typicode.com/users');
if (!response.ok) {
throw new Error('HTTP Error: ' + response.status);
}
const users = await response.json();
console.log(users); // Array of user objects
}
Understanding HTTP Status Codes in Fetch
- 200–299: Success (
response.ok === true) - 404: Not Found — the URL doesn't exist
- 500: Server Error — something broke on the backend
Common Response Methods
const data = await response.json(); // Parse as JSON object
const text = await response.text(); // Read as plain text string
const blob = await response.blob(); // Read as binary (images, files)
Tip
Always check response.ok before reading the body. A 404 or 500 response still " resolves " the fetch promise — response.ok is what tells you if the request actually succeeded.