Start the process of fetching a resource from the network and return a promise for the response.
The fetch() global function starts an HTTP request and returns a Promise that resolves to a Response object representing the server's reply. It is the modern replacement for XMLHttpRequest and is available in browsers, Service Workers, Web Workers, Deno, and recent versions of Node.js.
The response object provides streaming access to the body via methods like text(), json(), arrayBuffer(), and blob(). Critically, fetch() only rejects on network failures: HTTP error statuses such as 404 or 500 still resolve, so consumers must check response.ok or response.status manually.
A second options argument controls method, headers, body, mode, credentials, redirect, signal (for AbortController-based cancellation), and more. Combined with async/await, fetch() makes asynchronous HTTP code concise and easy to compose.
| Name | Description | Optional |
|---|---|---|
resource |
The URL string or Request object to fetch. | No |
options |
An object configuring method, headers, body, signal, and other request settings. | Yes |
const res = await fetch('https://api.example.com/users/1');
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const user = await res.json();
console.log(user.name);