Lesson 4 of 8
Fetch API in JavaScript
The browser provides the fetch() function to make HTTP requests. It returns a Promise, so you use .then() or async/await to handle the response. Always check if the response is OK before parsing JSON.
APIS
// Using fetch with promises
fetch("https://api.example.com/users")
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
// Using async/await (modern way)
async function getUsers() {
const response = await fetch("https://api.example.com/users");
const data = await response.json();
console.log(data);
}