How do you handle errors in async code, and what is an unhandled rejection?
Quick answer
Use try/catch with async/await or .catch on promise chains. An unhandled rejection is a rejected promise with no handler.
In detail
A rejected promise is the async version of a thrown error. With async/await wrap awaits in try/catch; with chains always end in `.catch`. Forgetting both produces an 'unhandled promise rejection', which can crash Node and is a common production bug. Note `Promise.all` rejects on the first failure — use allSettled to see all.
async function safe() {
try { return await fetch("/x").then(r => r.json()); }
catch (e) { report(e); return null; }
}
fetch("/x").then(r => r.json()).catch(report); // never forgetWhy interviewers ask this: Production-readiness signal.
This is 1 of 118+ questions in the JavaScript Interview Kit
Get every question with detailed answers, follow-ups and real code — plus coding challenges and a last-minute revision sheet. One-time payment, instant access.
⚡ Get the JavaScript Interview Kit → ₹299