Unhandled promise rejection: what it means and how to fix it
An unhandled promise rejection means a promise was rejected (an async function threw, or reject() was called) and no .catch(), second .then() argument or try/catch around an await handled it by the end of that turn of the event loop. Browsers log it as "Uncaught (in promise)" and fire the unhandledrejection event; Node.js 15 and later crash the process with exit code 1 by default. The fix is to await or return every promise and handle the error where you can act on it, not to silence the global event.
Steps
- Find the promiseRead the stack trace's first in-app frame; if the reason isn't an Error, search for where that value is rejected.
- Await or return itMake sure the promise is awaited, returned or passed to Promise.all, not dropped.
- Handle where you can actWrap the await in try/catch where you can show a message, retry or fall back, and report it.
- Reject with Error objectsReplace reject('message') with reject(new Error('message')) so the next report has a stack.
The exact messages, browser vs Node.js
In Chrome and Firefox (78 and later) the console shows the rejection reason with an (in promise) marker:
Uncaught (in promise) TypeError: Failed to fetchThe page keeps running. In Node.js, since v15.0.0 the default --unhandled-rejections mode is throw: if no unhandledRejection listener exists, the rejection is raised as an uncaught exception, the stack is printed and the process exits with code 1. A rejection with an Error prints that error. A rejection with anything else prints this (Node.js 22):
UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "boom".
at throwUnhandledRejectionsMode (node:internal/process/promises:392:7)
... {
code: 'ERR_UNHANDLED_REJECTION'
}The stack here is Node's internals, not yours: rejecting with a string or object loses the stack. That alone is a reason to always reject with new Error(...).
--unhandled-rejections= | Behaviour |
|---|---|
throw (default) | Emit unhandledRejection; if nothing listens, raise as an uncaught exception |
strict | Raise as an uncaught exception; emit unhandledRejection if that is handled |
warn | Always warn (UnhandledPromiseRejectionWarning: Error: boom), never crash |
warn-with-error-code | Emit the event; if nothing listens, warn and set exit code 1 |
none | Silence all warnings |
Before Node 15 the default was a warning, which is why older code bases are full of rejections that only now crash servers.
Causes, most common first
- A promise that isn't awaited or returned.
saveDraft(doc);inside an async function, oritems.forEach(async (i) => await save(i)):forEachignores the returned promises. .then()without.catch()at the end of a chain, or a.catch()handler that itself throws.- Fire-and-forget calls in event handlers, such as
onClick={() => api.save()}; nothing above them can catch. - Parallel promises awaited one by one.
const a = getA(); const b = getB(); await a; await b;Ifbrejects while the code is still waiting ona, it has no handler yet and is reported as unhandled. Useawait Promise.all([getA(), getB()]). fetchfailures.fetchrejects only on network errors (TypeError: Failed to fetch); a 500 resolves normally, so code that checksres.okbut not network errors leaves the rejection open.
Fixes that hold
Handle the error where you can do something about it:
// Before: rejection escapes the click handler
button.addEventListener('click', () => {
saveCart(cart);
});
// After
button.addEventListener('click', async () => {
try {
await saveCart(cart);
} catch (err) {
showToast('Couldn\'t save your cart');
Sentry.captureException(err); // handled, but you still want to know
}
});For loops, use for...of with await, or Promise.all / Promise.allSettled over map. For deliberate fire-and-forget, attach a handler explicitly: void refreshCache().catch(reportError). Turn on the @typescript-eslint/no-floating-promises lint rule to find un-awaited promises before they ship.
Global handlers are for reporting, not fixing. In the browser, window.addEventListener('unhandledrejection', (event) => …) receives a PromiseRejectionEvent with event.reason and event.promise; event.preventDefault() stops the console message. Rejections from cross-origin scripts never fire it. A matching rejectionhandled event fires when a handler is attached later. In Node, process.on('unhandledRejection', (reason, promise) => …) receives the same pair.
The mistake people make next: adding process.on('unhandledRejection', console.error) to stop the crashes. Because a listener now exists, Node's default throw mode no longer exits, and the process keeps running in whatever state the failed operation left it. Log, then exit and let your process manager restart it, unless you know the failure was harmless.
How Sentry reports unhandled rejections
In the browser, globalHandlersIntegration is on by default and captures both onerror errors and unhandled rejections (option onunhandledrejection). The event has handled: no and mechanism auto.browser.global_handlers.onunhandledrejection. A rejection with a non-Error value becomes Non-Error promise rejection captured with value: …, with no useful stack.
In Node.js, Bun and Deno, onUnhandledRejectionIntegration is on by default with mechanism auto.node.onunhandledrejection. Its mode defaults to warn: Sentry reports the event at level error, prints the This error originated either by throwing inside of an async function… warning, and the process keeps running, because Sentry's own listener counts as a handler. In our test with @sentry/node 11.19.0 the script exited with code 0 where plain Node would exit with 1. To keep crash-on-rejection behaviour:
Sentry.init({
dsn: 'https://<key>@o<orgId>.ingest.sentry.io/<projectId>',
integrations: [Sentry.onUnhandledRejectionIntegration({ mode: 'strict' })],
});With strict, the event is reported at level fatal and the process exits with code 1. To group these issues, search for error.mechanism:auto.node.onunhandledrejection or error.handled:false, then fix them by type. If the frames are minified, upload source maps first; React Native has its own rejection tracking, covered in Sentry for React Native.
Questions
Why did my Node.js app start crashing on unhandled promise rejections?
Node.js 15 changed the default --unhandled-rejections mode from a warning to throw, so an unhandled rejection now ends the process with exit code 1.
What does "Uncaught (in promise)" mean?
It is the browser's console message for an unhandled promise rejection: the rejection value follows, and the page keeps running.
Does Sentry stop my Node process from crashing on unhandled rejections?
By default, yes: onUnhandledRejectionIntegration uses mode warn, reports the error and keeps the process alive. Set mode to strict to exit as Node would.
Sources
- Window: unhandledrejection eventdeveloper.mozilla.org
- Command-line APInodejs.org
- GlobalHandlersdocs.sentry.io
- OnUnhandledRejectiondocs.sentry.io