Error Handling in Async
Async errors are rejected promises and exceptions thrown after an await. You handle them with try/catch around await, or with .catch on a promise chain — and you must attach one or the rejection is unhandled.
Detailed Explanation
A thrown error in a sync function unwinds the stack immediately. A thrown error in an async function rejects the promise that the function already returned to its caller. The caller only sees that rejection if they await or .catch. Otherwise the host fires unhandledrejection (browser) or unhandledRejection (Node). Node can exit the process.
try/catch works across await. It does not work across a forgotten await, and it does not wrap work you fired without waiting (doWork() as a dangling promise). void doWork() is a smell unless you attach .catch.
.catch on a combinator catches the combinator's rejection. Promise.all rejects on the first child rejection; the other children may still reject later and need their own handlers or allSettled. finally runs on settle and is where you release locks and abort() leftover work.
Error types matter. fetch only rejects on network failure, not on HTTP 404. You throw your own error after if (!res.ok). AbortError (or DOMException with name AbortError) is the normal outcome of cancel; do not report it as a crash.
Async stack traces in modern engines stitch the await chain. They are better than raw callbacks and still worse than a single sync stack. Put context on the error (err.userId = id) before rethrowing.
What it costs you: lost errors. The worst bug is a promise you did not wait for inside a map without Promise.all, or an event handler async () => { await fail(); } whose rejection the DOM ignores. Always handle at the boundary: the route handler, the event listener, the worker message handler.
Reach for try/catch in async functions when control flow is linear. Reach for .catch at the last consumer. Reach for allSettled when partial failure is data. Never empty-catch without a comment that names the reason.
Code Examples
async function readUser(id) {
try {
const res = await fakeFetch(id);
if (!res.ok) throw new Error(`http ${res.status}`);
return res.body;
} catch (err) {
err.id = id;
throw err;
}
}
function fakeFetch(id) {
if (id < 0) return Promise.reject(new Error("network"));
return Promise.resolve({ ok: id !== 0, status: id === 0 ? 404 : 200, body: { id } });
}
readUser(-1).catch((err) => console.log(err.message, err.id)); // => network -1async function loadAll(ids) {
const rows = await Promise.allSettled(ids.map((id) => readUser(id)));
return rows.filter((r) => r.status === "fulfilled").map((r) => r.value);
}button.addEventListener("click", () => {
readUser(7).catch((err) => {
console.error(err);
});
});Common Gotchas
An async event handler's rejection is easy to miss
attach `.catch` or wrap the body in `try/catch`. The browser will not show it as an uncaught sync exception.
`fetch` + 404 does not throw
check `res.ok` yourself.
`Promise.all` leftover rejections
failed siblings after the first reject can still be unhandled unless you `allSettled` or catch each child.
Cancel is an error path
treat `AbortError` as control flow, not as a defect.