Async Error Handling in JavaScript
Async errors demand explicit plumbing: awaited rejections throw into surrounding try/catch, unawaited chains need terminal .catch, and callback APIs need error-first handling — an unhandled rejection crashes modern Node outright. try/catch around await is the primary pattern; .catch chains cover promise style; window unhandledrejection handlers are last-resort telemetry, never logic.
Design rules: reject with Error objects (stacks included), never swallow silently (log or rethrow), separate expected failures (404s as values?) from exceptional ones, and ensure every promise path terminates in handling — floating promises are latent crashes.
try/catch awaits
try { await risky(); } catch (e) {...} — async throws land in ordinary catch blocks.
Terminal .catch chains
promise.then(...).catch(handle) — every floating chain ends in handling, or runtimes complain loudly.
Reject with Errors
reject(new Error("...")) carries stacks and types — string rejections debug terribly.
Never swallow silently
Empty catches hide failures until production — log, rethrow, or degrade explicitly, always.
async function risky(fail) { if (fail) throw new Error("boom"); return "fine";}async function main() { try { console.log(await risky(false)); } catch (e) { console.log("caught:", e.message); } try { await risky(true); } catch (e) { console.log("caught:", e.message); }}main();Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.