Fetch APIs and HTTP basics
Fetch APIs and HTTP basics
fetch sends HTTP requests and returns a promise for a Response. Check response.ok because HTTP errors do not automatically reject the promise. Respect CORS rules, validate returned data and show loading and error states.
Try it yourself
Change the example, run it, and explain the result in your own words. Then create one variation of your own.
Key idea
Use the example as a starting point, not something to memorize. Practise, inspect the result and make small improvements.
Code example
const response = await fetch("https://jsonplaceholder.typicode.com/todos/1");
if (!response.ok) throw new Error("Network response was not OK");
const todo = await response.json();
console.log(todo.title);