6. Async/Await
Intro: Promise Returning Is Tricky
fetch('https://pokeapi.co/api/v2/pokemon/pikachu')
.then((response) => {
if (!response.ok) {
throw Error(`Fetch failed. ${response.status} ${response.statusText}`);
}
const readingPromise = response.json();
})
.then((data) => {
console.log(data); // prints undefined
})
.catch((error) => {
console.log("Error caught!");
console.log(error.message);
})The benefits of async/await
async/awaitFetching "Synchronously" with Async/Await
Handling Errors with Try/Catch
Last updated