Asyncintermediate
The Fetch API
The modern, Promise-based browser API for making HTTP requests, replacing XMLHttpRequest for most use cases.
The Fetch API is the standard, Promise-native way to make HTTP requests, replacing the older XMLHttpRequest. Since network requests are one of the most common uses of async JavaScript, fetch fluency is a near-universal interview topic.
fetch is like ordering delivery: the promise resolving is the doorbell ringing — it doesn't mean the food is good or that you've opened the box yet.
Key Concepts
1
Calling fetch(url, options) returns a Promise that resolves once headers arrive, and only rejects on network-level failures — not on HTTP error status codes like 404 or 500. You must manually check response.ok and throw yourself if you want failed statuses treated as errors.
2
Once you have the response, reading the body (response.json(), .text(), etc.) is a second asynchronous step, since the body may still be streaming.
3
The options object supports method, headers, body, credentials, and an AbortSignal for cancellation via AbortController, the standard mechanism for canceling an in-flight fetch.