Async & Promisesmedium
Unhandled promise rejections: causes and fixes
Explain what unhandled promise rejections are, why they happen, and how to systematically prevent them in app code.
Asked at Meta, Amazon, Google
Answer
Interview explanation:
- An unhandled rejection means a promise rejected without a rejection handler.
- In modern runtimes this is noisy and can crash processes (Node settings dependent).
Common causes:
- Fire-and-forget promise without .catch.
- Missing await in try/catch (error escapes scope).
- Missing return in .then chain.
Examples: void riskyAsync().catch(reportError) // explicit fire-and-forget handling
async function handler() { try { await riskyAsync() } catch (err) { reportError(err) } }
Prevention checklist:
- Always return/await promises from functions.
- Add a terminal .catch on top-level chains.
- Wrap UI event async handlers in try/catch.
- Centralize logging for unexpected async failures.