node
async
ai_generated
true
UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block
ID: node/err-unhandled-rejection
95%Fix Rate
96%Confidence
50Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 20 | active | — | — | — |
Root Cause
A Promise rejected and no .catch() or try/catch in async function was present to handle it.
genericWorkarounds
-
95% success Add try/catch blocks around await calls in async functions
try { await fn() } catch(e) { handleError(e) }Sources: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
-
93% success Add .catch() to all Promise chains
promise.then(result => ...).catch(err => ...)
Dead Ends
Common approaches that don't work:
-
Adding process.on('unhandledRejection', () => {}) to silence it
85% fail
Hides bugs; since Node 15 unhandled rejections crash the process by default
-
Converting all async functions to callbacks
90% fail
Massive refactor that introduces callback hell; not a real fix
Error Chain
Leads to: