Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Async operations can fail - network issues, invalid data, server errors. Always handle errors to prevent crashes and provide good user experience!
Add .catch() to handle errors from any promise in the chain
fetch('/api/data')
.then(response => response.json())
.then(data => {
console.log('Success:', data);
})
.catch(error => {
console.log('Error:', error);
});Handle errors in promise chains
Use try/catch to handle errors when using async/await
async function loadUser() {
try {
const response = await fetch('/api/user');
const user = await response.json();
console.log('User:', user);
} catch (error) {
console.log('Error:', error);
}
}Clean error handling
Connection failed, timeout, offline
404 not found, 500 server error, 401 unauthorized
Invalid JSON, corrupt data
Business logic failures, invalid input
Identify and handle specific errors
Automatically retry failed requests with increasing delays
async function fetchWithRetry(url, retries = 3) {
for (let i = 0; i < retries; i++) {
try {
const response = await fetch(url);
return await response.json();
} catch (error) {
if (i === retries - 1) throw error;
await delay(1000 * (i + 1));
}
}
}Retry with exponential backoff