Fetch
ELI5 — The Vibe Check
Fetch is the modern, built-in JavaScript way to make HTTP requests to APIs. You tell it a URL, it goes and gets the data, and you handle the response. It replaced the older, uglier XMLHttpRequest. It returns a Promise, so you can use async/await with it.
Real Talk
The Fetch API is a modern browser API for making HTTP requests. It returns a Promise that resolves to a Response object. Unlike XMLHttpRequest, Fetch uses Promises natively, supports streaming, and provides a cleaner interface. Note: Fetch does not reject on HTTP error status codes (4xx/5xx) — you must check response.ok manually.
Show Me The Code
// Basic GET request
const res = await fetch('https://api.example.com/users')
if (!res.ok) throw new Error(`HTTP error: ${res.status}`)
const users = await res.json()
// POST with JSON body
const res2 = await fetch('/api/users', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'Alice' })
})
When You'll Hear This
Fetch won't throw on 404 — always check response.ok.,Use AbortController to cancel fetch requests.,Fetch is now available in Node.js 18+ natively.
Related Terms
Ajax
Ajax is the technique of loading data from a server in the background without refreshing the whole page.
Async/Await
Async/await is syntactic sugar that makes Promises look like normal, readable code. Instead of chaining .then().then().
JSON (JavaScript Object Notation)
JSON is the universal language the internet uses to pass data around. It looks like a JavaScript object — curly braces, key-value pairs.
Promise
A Promise is JavaScript's way of saying 'I'll give you a value eventually — it's not ready yet, but I promise.