Error Handling in
Async Code
Master try/catch with await to write robust asynchronous JavaScript that handles failures gracefully and never silently swallows errors.
What is Asynchronous Code?
JavaScript is single-threaded: it executes one line at a time. But some operations: fetching data from servers, reading files, or waiting for timers: take time. Instead of blocking the entire program, JavaScript lets these operations run in the background and notifies you when they finish.
Promises: The Foundation
A Promise is an object that represents a value that may not exist yet. It has three states:
async/await: Cleaner Syntax
The async/await syntax lets you write asynchronous code that reads like synchronous code. The async keyword marks a function as asynchronous, and await pauses execution until a promise settles.
Why Errors Happen in Async Code
Async operations interact with the outside world: networks, servers, filesystems. Any of these can fail unpredictably. Your code must anticipate and handle these failures.
The Core Pattern: try/catch with await
Wrap your await expressions inside a try block. If the promise rejects, execution jumps to the catch block: just like synchronous error handling.
The Syntax in Full
.then/.catch vs try/catch
Both approaches handle async errors, but try/catch with await produces cleaner, more readable code: especially when you have multiple sequential async operations.
| .then/.catch | try/catch + await | |
|---|---|---|
| Readability | Callback chains can nest deeply | Reads top-to-bottom like sync code |
| Error scope | Each .catch() handles its own chain | One catch block covers all awaits |
| Debugging | Stack traces can be fragmented | Clearer stack traces |
| Control flow | Harder to use if/else, loops | Works naturally with all control flow |
How to Apply the Pattern
Example 1: Basic Fetch
Example 2: Multiple Awaits
Example 3: Parallel with Promise.all
Granular Error Handling
Sometimes you want to handle errors from each await differently instead of one blanket catch. You can use nested try/catch or check the error type inside catch.
The Error Object
When catch receives an error, it is an Error object with useful properties for debugging.
Common Mistakes
Summary: Error Handling Patterns
| Pattern | Best For | Catches Errors? |
|---|---|---|
| try/catch + await | Sequential async operations | ✓ Yes |
| .then().catch() | Single promise chains | ✓ Yes |
| Promise.all + try/catch | Parallel operations (fail-fast) | ✓ Yes |
| Promise.allSettled | Parallel operations (partial results) | Per-promise status |
| await without try/catch | Never do this in production | ✕ No |
Question 1 of 5
What happens if you use await inside a function that is not marked as async?
Question 2 of 5
Consider this code:
async function getData() {
try {
const res = await fetch('/api/data');
const data = await res.json();
return data;
} catch (err) {
console.error(err);
}
}
If the server returns a 404 Not Found response, what happens?
Question 3 of 5
What is the purpose of the finally block in a try/catch/finally statement?
Question 4 of 5
Consider this code:
async function loadPage() {
try {
const user = await fetchUser();
const posts = await fetchPosts(user.id);
render(user, posts);
} catch (err) {
showError(err.message);
}
}
If fetchUser() rejects with an error, which of the following is true?
Question 5 of 5
Which of the following is a dangerous anti-pattern in async error handling?