Async error handling
Throw inside async
A throw inside an async function rejects the returned promise. The caller sees the same error through await or catch. This lets async code use plain throw instead of manual reject calls. The catch prints the message of the error, not the error object, so the output is one line.
async function load(): Promise<string> { throw new Error("missing"); } load().catch((err: unknown) => { console.log((err as Error).message); });
missing
Catch per call
Handle each risky await where recovery is possible. A small try block around one call keeps the fix close to the cause. Return a default or rethrow when the caller must decide. The load helper is repeated here, so the block runs on its own, and the program calls withDefault, so the error message and the default print one after the other.
async function load(): Promise<string> { throw new Error("missing"); } async function withDefault(): Promise<string> { try { return await load(); } catch (err: unknown) { console.log((err as Error).message); return "guest"; } } async function main(): Promise<void> { const name = await withDefault(); console.log(name); } main();
missing
guest
Top-level guard
Every program needs one last guard that logs fatal errors. Attach catch to the top promise or wrap main in try and catch. Without it a rejection becomes an unhandled crash with a thin trace. The load and withDefault helpers are repeated here, and main throws after the guarded load, so the guard prints a line of its own.
async function load(): Promise<string> { throw new Error("missing"); } async function withDefault(): Promise<string> { try { return await load(); } catch (err: unknown) { console.log((err as Error).message); return "guest"; } } async function main(): Promise<void> { const name = await withDefault(); console.log(name); throw new Error("fatal"); } main().catch((err: unknown) => { console.log((err as Error).message); });
missing
guest
fatal
Retry with backoff sketch
Transient faults often clear on a second try after a short pause. Retry a few times with growing delays between attempts. Rethrow the last error when attempts run out so the guard above still sees it. The program calls retry with two tries against a load that always fails, so the catch prints the last error after the two pauses.
function delay(ms: number): Promise<void> { return new Promise((resolve) => { setTimeout(resolve, ms); }); } async function load(): Promise<string> { throw new Error("missing"); } async function retry(tries: number): Promise<string> { let last: unknown = null; for (let i = 0; i < tries; i++) { try { return await load(); } catch (err: unknown) { last = err; await delay(100 * (i + 1)); } } throw last; } async function main(): Promise<void> { try { const value = await retry(2); console.log(value); } catch (err: unknown) { console.log((err as Error).message); } } main();
missing
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a throw inside an async function do?
Answer
It rejects the promise the function returns
An async function turns a throw into a rejection, and the caller meets it at await or in a catch handler.
-
Where does a catch for one risky await belong?
Answer
Around that await, close to the cause
A small try block around one call keeps the fix next to the call that can fail and leaves other work alone.
-
Why does every program need a top-level guard?
Answer
It catches the rejection that nothing else handled
Without a catch on the top promise a rejection is unhandled, and the process crashes with a thin trace.
-
What should a retry loop do when its attempts run out?
Answer
Rethrow the last error so the guard sees it
The loop cannot recover on its own, so it hands the last error back to the guard that knows what to do.
-
What does this program print?
async function load(): Promise<string> { throw new Error("missing"); } async function withDefault(): Promise<string> { try { return await load(); } catch (err: unknown) { return "guest"; } } withDefault().then((name) => { console.log(name); });Answer
guest
The load call rejects, the catch returns the default, and the chain prints that default.
-
What does this program print?
async function main(): Promise<void> { throw new Error("fatal"); } main().catch((err: unknown) => { console.log((err as Error).message); });Answer
fatal
The guard takes the rejection and prints the message of the error, without the class name.
-
What does this program print?
async function load(): Promise<string> { throw new Error("missing"); } async function withDefault(): Promise<string> { try { return await load(); } catch (err: unknown) { console.log((err as Error).message); return "guest"; } } async function main(): Promise<void> { const name = await withDefault(); console.log(name); } main();Answer
missing / guest
The catch logs the message first and then returns the default, which main prints after the await.
-
What does this program print?
function delay(ms: number): Promise<void> { return new Promise((resolve) => { setTimeout(resolve, ms); }); } let attempts = 0; async function load(): Promise<string> { attempts += 1; if (attempts < 2) { throw new Error("busy"); } return "ready"; } async function retry(tries: number): Promise<string> { let last: unknown = null; for (let i = 0; i < tries; i++) { try { return await load(); } catch (err: unknown) { last = err; await delay(1); } } throw last; } async function main(): Promise<void> { console.log(await retry(3)); } main();Answer
ready
The first attempt fails, the pause gives the second attempt its chance, and that call returns the value the program prints.
-
What does the compiler say about this program?
try { throw new Error("x"); } catch (err: Error) { console.log(err.message); }Answer
error TS1196: Catch clause variable type annotation must be 'any' or 'unknown' if specified.
A catch clause may be annotated only as any or unknown, because the thrown value can be anything.
-
What goes wrong when a retry loop throws inside its catch instead of after the loop?
Answer
It gives up after the first failure, so the pause never helps
A throw inside the catch leaves the loop at once, so the next attempt never runs and the growing pause is wasted.
Article author: Arthur Isaev