Quiz Entry - updated: 2026.06.20
How do you handle errors in async/await code?
Wrap the awaited calls in a try/catch block, because a rejected Promise throws an exception at the await.
async function loadData() {
try {
const response = await fetch('/api/data');
const data = await response.json();
return data;
} catch (error) {
console.error('Failed to load:', error);
}
}
The catch block handles both network errors and rejected Promises.