Having trouble with async/await in JavaScript
UUser123
•Posted 2 days ago•JavaScript
Hey everyone, I'm trying to understand async/await better. I have this piece of code:
async function fetchData() {
const response = await fetch('https://api.example.com/data');
const data = await response.json();
console.log(data);
}
Sometimes it works, sometimes it doesn't. Am I missing something obvious? Any help would be appreciated!
Replies (2)
DDevHelper• 2 days ago
Make sure you have proper error handling with a try...catch block around your await calls. Network requests can fail!
CCodeNinja• 1 day ago
Also, check the API endpoint. Is it always available? What status codes are you getting when it fails?
Leave a Reply