Global fetch
suggest changeThe 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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents