Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

onrejectionhandled — handling a Promise rejection later

The body attribute onrejectionhandled registers a handler for the Window rejectionhandled event: a previously unhandled Promise rejection has subsequently acquired a handler.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<body onrejectionhandled="handler(event)">
window.onrejectionhandled = handler;

onrejectionhandled on body is an event-handler attribute for Window, not an ordinary element-local event. Its corresponding property is window.onrejectionhandled.

The rejectionhandled event occurs when a previously reported unhandled rejection acquires handling. event.promise identifies the promise, and event.reason contains its rejection reason. Registering this event handler does not itself handle the rejection; the promise needs handling such as .catch().

The example waits for unhandledrejection, then attaches .catch() in a later task. preventDefault() suppresses default reporting of the first event but does not handle the promise. The page should display handled: demo.

Examples

<!doctype html>
<meta charset="utf-8">
<title>rejectionhandled demo</title>
<body onrejectionhandled="if (event.promise === window.demoPromise) document.getElementById('result').textContent = 'handled: ' + event.reason">
<pre id="result">waiting</pre>
<script>
window.addEventListener('unhandledrejection', (event) => {
  if (event.promise !== window.demoPromise) return;
  event.preventDefault();
  setTimeout(() => {
    event.promise.catch(() => {});
  }, 0);
});
window.demoPromise = Promise.reject('demo');
</script>
</body>

Result

handled: demo

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources