fetch
Use the promise-based API with options for methods and headers.
What you will be able to do
- Pass method, headers and body in options.
- Parse the body once.
- Use AbortController for cancellation.
- Handle network errors with try/catch.
In plain English
fetch takes a URL and an options object. It resolves when headers arrive, not when the body finishes. Parse with json(), text() or blob() exactly once. Use AbortController to cancel.
const controller = new AbortController()
const response = await fetch('/api/lessons', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title: 'CSS' }),
signal: controller.signal,
})
const data = await response.json()Your turn
EasySend JSON
Write a fetch POST with a JSON body and Content-Type header.
JAVASCRIPT · starter
fetch('/api/lessons')Check your understanding before moving on.
Knowledge checkQuestion 1
When does fetch reject?
Select an answer to continue
Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.