Why this matters
Uncaught rejections cause silent failures and inconsistent states. Explicit handling makes failures observable and recoverable.
Every awaited async operation must be guarded with error handling. Use try/catch for await and .catch for promise chains; never leave rejections unhandled.
Uncaught rejections cause silent failures and inconsistent states. Explicit handling makes failures observable and recoverable.
Side-by-side examples engineers can pattern-match during review.
async function load(){ const data = await fetch(url).then(r=>r.json()); return data.items; }async function load(){ try{ const r = await fetch(url); if(!r.ok) throw new Error(`HTTP ${r.status}`); return (await r.json()).items; } catch(err){ logger.error('load failed',{op:'load',url,err}); throw err; }}await fetch(url).then(r=>r.json())try { const r = await fetch(url) } catch (e) { logger.error(e) }From the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.