error-handling

unhandledrejection event

Fires when a promise rejects and has no rejection handler attached by the end of the current microtask cycle. Provides a last-chance hook to log or report the error before the runtime warns or exits.

window.addEventListener('unhandledrejection', handler)   // Node: process.on('unhandledRejection', handler)

Parameters

Parameter Purpose
event.reason The rejection reason (browser event)
event.preventDefault() Suppress the default warning in browsers
Node: (reason, promise) Node passes reason and the rejected promise

Examples

window.addEventListener('unhandledrejection', event => {
  console.error('Unhandled:', event.reason);
  event.preventDefault();
});

Browser global handler for stray promise rejections.

process.on('unhandledRejection', (reason, promise) => {
  console.error('Unhandled Rejection:', reason);
});

Node.js equivalent — since Node 15 unhandled rejections crash the process by default.

// Triggers the event
Promise.reject(new Error('lost'));
// (No .catch anywhere, so unhandledrejection fires)

Attaching a .catch later within the same microtask cancels it via 'rejectionhandled'.

Gotcha

Node 15+ crashes on unhandled rejections by default — always attach a handler in production. This is a safety net, not a substitute for local .catch or try/catch.

Related

← All JS Promise reference · async/await vs promises vs callbacks