@fazzaamiarso
Posted
Hi Prabu! Great work overall!
You can make your code more robust implementing errror handling on fetch. Here is my implementation.
async function getData() {
const res = await fetch('https://api.adviceslip.com/advice');
if(!res.ok) throw Error("There is something wrong!"); //throw error if fetch failed
return await res.json();
}
const adviceCard = document.querySelector("main");
const udpdateDOM = (slip) =>{
adviceCard.querySelector("span").innerText = `ADVICE #${slip.id}`;
adviceCard.querySelector("h1").innerText = `"${slip.advice}"`;
}
async function setAdvice() {
//catch the error down here
try {
const { slip } = await getData();
udpdateDOM(slip);
}catch (err) {
console.error(err);
}
}
Marked as helpful