fetch()

Starts a network request and returns a promise resolving to a Response.

Since ES2015 (ES6) Spec ↗

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

EnvironmentSince version
chrome 42
firefox 39
safari 10.1
edge 14
node 18.0

See also