Manuals / JavaScript / Chapter 8
C · Async · intermediate · Week 5 · Chapter 8 of 14
Fetch, HTTP & error UX
fetch(url) returns a Promise resolving to Response. Check response.ok — fetch does not reject on 404. Parse JSON with .json(). Build loading, success, and error UI states. Same patterns apply in Node with fetch (built-in since v18).
Path progress51%
Step 1 of 5
GET request
const res = await fetch(url). if (!res.ok) throw new Error(res.status). const data = await res.json().
Example
async function getPost(id) {
const res = await fetch(`https://jsonplaceholder.typicode.com/posts/${id}`)
if (!res.ok) throw new Error(`HTTP ${res.status}`)
return res.json()
}Do this now
Fetch https://jsonplaceholder.typicode.com/posts/1 and log title.
Was this step clear?
Chapter learning outcomes
- fetch API
- HTTP status codes
- Loading/error UI states
- Headers & POST bodies
- AbortController
Clear these before you leave
Side quest
Post browser
Small app: fetch posts list, click to view detail, search with debounced fetch, full loading/error states.