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

Also available as: JSON · Markdown
95%Fix Rate
96%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
20 active

Root Cause

A Promise rejected and no .catch() or try/catch in async function was present to handle it.

generic

Workarounds

  1. 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

  2. 93% success Add .catch() to all Promise chains
    promise.then(result => ...).catch(err => ...)

Dead Ends

Common approaches that don't work:

  1. Adding process.on('unhandledRejection', () => {}) to silence it 85% fail

    Hides bugs; since Node 15 unhandled rejections crash the process by default

  2. Converting all async functions to callbacks 90% fail

    Massive refactor that introduces callback hell; not a real fix

Error Chain

Leads to: