Common Error Handling Mistakes
You'll learn: The typical pitfalls developers encounter when handling errors with async/await, and how to avoid writing fragile code.
Even though try/catch makes async error handling feel intuitive, certain mistakes can sneak in and cause hard-to-debug issues. Let's examine the most common traps.
Forgetting to Await Before Throwing
If you throw an error without awaiting a promise first, the error happens synchronously while the promise rejection happens asynchronously—creating two separate error paths:
async function badExample() {
const promise = fetch('/api/data'); // No await!
throw new Error('This throws immediately');
// The fetch error goes unhandled
}
Always await promises before conditional throwing, or the promise rejection escapes your try/catch.
Catching Too Broadly
Wrapping everything in one giant try/catch means you can't distinguish between different error sources:
async function tooBoard() {
try {
await stepOne();
await stepTwo();
await stepThree();
} catch (error) {
// Which step failed? Hard to tell!
console.log('Something broke');
}
}
Be selective: catch only what you can meaningfully handle.
Swallowing Errors Silently
The worst mistake is catching errors but doing nothing with them:
async function silent() {
try {
await riskyOperation();
} catch (error) {
// Empty catch — error vanishes!
}
}
This is like hiding a fire alarm under a pillow. At minimum, log the error. Better yet, handle it properly or re-throw it.
Not Handling Async Function Rejections
Remember: async functions return promises. If you don't catch them at the call site, they become unhandled promise rejections:
async function task() {
throw new Error('Oops');
}
task(); // Unhandled rejection!
Always attach .catch() or use try/catch when calling async functions.
Key Takeaway: Effective error handling requires discipline—always await before conditionally throwing, catch selectively rather than broadly, never swallow errors silently, and handle rejections at every call site.