Uncaught Errors in Async Functions
You'll learn what happens when an async function throws an error that nobody catches—and why it matters.
The Unhandled Rejection Problem
When an async function throws an error (or awaits a rejected Promise) and there's no try/catch block inside the function and no .catch() handler attached when calling it, that error doesn't crash your program immediately. Instead, it becomes an unhandled rejection.
Think of it like throwing a ball in an empty room—nobody's there to catch it, so it just bounces around until someone notices the noise.
What Actually Happens
async function riskyOperation() {
throw new Error('Something went wrong!');
// No try/catch here
}
// Called without .catch()
riskyOperation();
console.log('This still runs!');
In this example:
- The error is thrown inside
riskyOperation - Since it's an async function, the error becomes a rejected Promise
- No
.catch()handler is attached, so the rejection is unhandled - The script continues executing—
console.logstill runs! - The JavaScript environment logs an "Unhandled Promise Rejection" warning
Why This Is Dangerous
Unhandled rejections can hide bugs in your code. Your application might appear to work while silently failing in the background. In Node.js, unhandled rejections can even terminate the entire process in stricter configurations.
The Fix
Always handle errors from async functions:
riskyOperation().catch(err => {
console.error('Caught:', err.message);
});
Key Takeaway: An uncaught error in an async function becomes an unhandled Promise rejection, which continues executing but logs warnings—always attach .catch() or use try/catch to prevent silent failures.