freecoding.school100% FREE · NO SIGNUP
Await StationISSUE #7 of 26

error handling · try / catch around await

PromissaVSRace Condition Rex
Promissa saysHandle async errors by wrapping await in try/catch — the same pattern as synchronous code.

A rejected promise that you await throws, so wrap it in try { await … } catch (err) { … }. finally works too. This unifies sync and async error handling under one familiar structure. Without try/catch (or a .catch), a rejection becomes an unhandled promise rejection.

Power-ups you unlock

Race Condition Rex attacks — common mistakes

Boss battleawait a rejecting promise inside try/catch and report the caught error.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>const fail=()=>Promise.reject(new Error('nope'));
(async()=>{ try{ await fail(); }catch(e){ document.getElementById('o').textContent='caught: '+e.message; } })();</script></body></html>
▶ Open the interactive comic issue
‹ Async / Await · Sugar For PromisesFetch · The Modern Http Client ›