fetch()
Starts a network request and returns a promise resolving to a Response.
Syntax
fetch(resource, options)
Returns
Promise — A promise that resolves to a `Response` object.
Examples
const res = await fetch("https://example.com/data.json");
const data = await res.json();
console.log(data.id);
Output
1
await fetch("/api", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Ada" }),
});
Notes
- The promise only rejects on network failure, not on HTTP error
statuses; check `res.ok` or `res.status`.
- Read the body with `res.json()`, `res.text()`, `res.blob()`, etc.
- Available in browsers and in Node.js 18+.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 42 |
| firefox | 39 |
| safari | 10.1 |
| edge | 14 |
| node | 18.0 |