Not Handling Rejections
You'll learn why forgetting to handle Promise rejections in async functions can crash your app or hide critical bugs.
The Silent Danger
When you use await without try/catch, a rejected Promise becomes an unhandled rejection. In some environments, this crashes your application. In others, it fails silently — your code stops executing, but you never know why.
Think of it like ordering food delivery but never checking if it arrived. The driver might have gotten lost (rejection), but you're still sitting there hungry, wondering why nothing happened.
What Happens Without Error Handling
async function fetchUser(id) {
const response = await fetch(`/api/user/${id}`); // What if this fails?
const data = await response.json();
return data;
}
// Calling without handling rejections
fetchUser(123); // If fetch fails, the error disappears into the void
If the network request fails, the rejection propagates up... to nowhere. Your function stops executing, but the calling code has no idea anything went wrong.
The Safe Way
Always handle rejections with try/catch or by chaining .catch():
async function fetchUser(id) {
try {
const response = await fetch(`/api/user/${id}`);
const data = await response.json();
return data;
} catch (error) {
console.error('Failed to fetch user:', error);
return null; // Or throw, or return a default value
}
}
Or when calling:
fetchUser(123).catch(error => {
console.error('User fetch failed:', error);
});
Why This Matters
Unhandled rejections are like unchecked exceptions in other languages. In Node.js, they can crash your server. In browsers, they clutter the console and may soon crash pages too. They represent bugs you didn't think about — the network failing, APIs returning errors, parsing invalid data.
Key Takeaway: Every async function with await needs error handling, either with try/catch inside or .catch() when called — unhandled rejections are production bugs waiting to happen.