Advertisement
Beginner JavaScript, Networking: Fetch, XHR, WebSockets & CORS

How do you make a basic GET request with fetch()?

Short answer

fetch(url) returns a Promise that resolves to a Response object once headers arrive; you then call .json() (or .text(), etc.) — itself also returning a Promise — to read and parse the actual body.

Save JavaScript mock interview
Advertisement
AModel answer
fetch(url) returns a Promise that resolves to a Response object once headers arrive; you then call .json() (or .text(), etc.) — itself also returning a Promise — to read and parse the actual body.
const response = await fetch('/api/users');
const users = await response.json();
console.log(users);
Real-world example
Loading a list of products from an API when a page first renders.
Likely follow-ups
  • Does fetch() reject its Promise on an HTTP error status like 404 or 500?
Related topics