Beginner Topic 15 of 51 Web Development
Fetch API
Browser interface for making HTTP requests.
How it works
The Fetch API is the modern browser interface for making HTTP requests, replacing XMLHttpRequest. fetch(url, options) returns a promise that resolves to a Response object, from which you read the body with methods such as json() and text(). Options set method, headers, body, credentials, cache mode and an AbortSignal for cancellation. It supports streaming bodies and works in service workers and in Node.js.
Step-by-step walkthrough
- fetch("/api/items") sends a request and returns a promise.
- The script continues without blocking.
- The server answers with JSON.
- The promise resolves and the script calls response.json().
- The page renders the data, and HTTP errors need explicit checks.
Where it is used
Loading data from REST APIs, submitting forms asynchronously, uploading files, calling back-end endpoints in single-page applications and prefetching resources use fetch.
Common pitfall
The promise rejects only on network failure, not on HTTP error codes like 404 or 500, so check response.ok. Forgetting to send credentials or set Content-Type headers causes authentication and parsing issues. CORS restrictions block cross-origin calls without server permission. There is no built-in timeout; use AbortController. Handle errors and loading states in the interface.
Where it fits in the course
Fetch API is topic 15 of 51 in Web Development, rated Beginner. It follows Events. The next topic is JSON.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Web Development quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Web Development quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Fetch API?
Where do people use Fetch API?
What is the most common mistake with Fetch API?
What should I learn before this topic?
How can I practice it?
Is Fetch API hard to learn?
How long does it take to learn Fetch API?
Which topics are related to Fetch API?
What should I study after Fetch API?
Related topics
- Events (Web Development)
- JSON (Web Development)
- DOM (Web Development)
- Browser DevTools (Web Development)
- Fetch-Decode-Execute Cycle (Computer Architecture & Logic)