Global fetch

suggest change

The global fetch() function requests a resource and fulfills with a Response object:

async function loadResource() {
  const response = await fetch('/path/to/resource.json');

  if (!response.ok) {
    throw new Error(`Request failed with status ${response.status}`);
  }

  const data = await response.json();
  console.log(data);
}

The response exposes the status and headers immediately. Methods such as json() and text() asynchronously read and decode the body. Remember that response.ok is a Boolean property, not a method.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents