JavaScript Async/Await Error Handling — Try/Catch Examples
Try/catch with async functions
The live editor loads here with JavaScript enabled.
Run in Playground ↗The original source and explanation are available below.
Changes here stay separate from your main playground. Use Open in TryJS inside the editor to keep working with your changes.
Useful? Help keep TryJS free and independent.
Support TryJSWhat to expect
Both success and simulated failure are handled, and cleanup runs in either case.
About this example
async/await is syntactic sugar over promises, and so is its error handling: a rejected promise becomes a thrown exception inside the async function, catchable with try/catch just like synchronous errors. The catch block receives whatever the rejected promise was rejected with — usually an Error instance, but technically any value. Combined with await, this gives async code the same clean error-handling shape as synchronous code.
How It Works
await turns rejection into throw
When you await a promise that rejects, the await expression throws synchronously inside the async function. Wrapping it in try/catch catches the error the same way you'd catch a regular throw.
One try, multiple awaits
You can put several awaits inside the same try block. The first one that rejects jumps to catch — subsequent awaits are skipped. Use separate try blocks only when each error needs different handling.
finally runs no matter what
A finally block runs after try (whether it completed or threw) and after catch. Perfect for cleanup — closing connections, releasing locks, hiding spinners.
Unhandled rejections still happen
If you don't await a promise and it rejects, you get an unhandledRejection. Always await promises you expect to potentially fail, or attach a .catch(). Silent rejections are the async equivalent of silently-swallowed exceptions.
Things to watch for
- Forgetting to await a promise inside try — a rejection won't be caught because the function has already returned.
- Swallowing errors with an empty catch block — the error is gone but so is any diagnostic.
- Assuming catch(e) gives you an Error — it could be any rejected value, including a string or undefined.
- Writing try/catch around a Promise.all where you only care if at least one rejected — use allSettled if you want per-promise outcomes.
When to use it
Always handle expected failure modes with try/catch (network errors, missing data, permission denied). Let truly unexpected errors bubble up to a higher-level handler — don't catch errors you don't know how to handle.
Read the original source
This is the starting code. Your edits stay in the live editor above.
JavaScript
async function fetchData(shouldFail) {
await new Promise(r => setTimeout(r, 100));
if (shouldFail) throw new Error("Network error");
return { data: [1, 2, 3] };
}
(async () => {
// Success
try {
const result = await fetchData(false);
console.log("Success:", result);
} catch (err) {
console.error("Failed:", err.message);
} finally {
console.log("cleanup 1");
}
// Failure
try {
const result = await fetchData(true);
console.log("Success:", result);
} catch (err) {
console.error("Failed:", err.message);
} finally {
console.log("cleanup 2");
}
})();Questions about this example
How do I handle errors in async/await?
Wrap the await expression in a try/catch block. A rejected promise becomes a thrown exception inside the async function, so try/catch catches it exactly like a synchronous throw.
Can I use try/catch with multiple awaits?
Yes. A single try block can wrap as many awaits as you want. The first rejection jumps to the catch block, and subsequent awaits are skipped. Use separate try blocks only when each error requires different handling.
What is an unhandled promise rejection?
A promise that rejects and has no catch handler or try/catch wrapping an await. Node and browsers emit an 'unhandledRejection' event; in strict environments it terminates the process. Always await or .catch() every promise you create.
Should I always use try/catch inside async functions?
Only at layers where you can handle the error — retry, show a user-facing message, log it, fall back to a default. Don't wrap every line in try/catch 'just in case'; let errors bubble up to a layer that knows what to do.