From bf6ace99cfc5e9bd2631265a0480e4f374202c49 Mon Sep 17 00:00:00 2001 From: Florian Scholz Date: Fri, 22 Apr 2022 14:42:32 +0200 Subject: [PATCH] Fix promise rejection events (#15244) --- files/en-us/_redirects.txt | 6 +- files/en-us/_wikihistory.json | 23 ------- .../promiserejectionevent/promise/index.md | 2 +- .../promiserejectionevent/index.md | 5 +- .../api/promiserejectionevent/reason/index.md | 2 +- files/en-us/web/api/window/index.md | 6 -- .../window/rejectionhandled_event/index.md | 51 +++++++++------- .../window/unhandledrejection_event/index.md | 61 +++++++++---------- .../web/api/windoweventhandlers/index.md | 5 -- .../onrejectionhandled/index.md | 42 ------------- .../onunhandledrejection/index.md | 43 ------------- 11 files changed, 67 insertions(+), 179 deletions(-) delete mode 100644 files/en-us/web/api/windoweventhandlers/onrejectionhandled/index.md delete mode 100644 files/en-us/web/api/windoweventhandlers/onunhandledrejection/index.md diff --git a/files/en-us/_redirects.txt b/files/en-us/_redirects.txt index febbda7e3e86d76..34fe196d300a877 100644 --- a/files/en-us/_redirects.txt +++ b/files/en-us/_redirects.txt @@ -9735,13 +9735,13 @@ /en-US/docs/Web/API/Window/onmouseover /en-US/docs/Web/API/GlobalEventHandlers/onmouseover /en-US/docs/Web/API/Window/onmouseup /en-US/docs/Web/API/GlobalEventHandlers/onmouseup /en-US/docs/Web/API/Window/onpopstate /en-US/docs/Web/API/Window/popstate_event -/en-US/docs/Web/API/Window/onrejectionhandled /en-US/docs/Web/API/WindowEventHandlers/onrejectionhandled +/en-US/docs/Web/API/Window/onrejectionhandled /en-US/docs/Web/API/Window/rejectionhandled_event /en-US/docs/Web/API/Window/onreset /en-US/docs/Web/API/GlobalEventHandlers/onreset /en-US/docs/Web/API/Window/onresize /en-US/docs/Web/API/GlobalEventHandlers/onresize /en-US/docs/Web/API/Window/onselect /en-US/docs/Web/API/GlobalEventHandlers/onselect /en-US/docs/Web/API/Window/onstorage /en-US/docs/Web/API/Window/storage_event /en-US/docs/Web/API/Window/onsubmit /en-US/docs/Web/API/GlobalEventHandlers/onsubmit -/en-US/docs/Web/API/Window/onunhandledrejection /en-US/docs/Web/API/WindowEventHandlers/onunhandledrejection +/en-US/docs/Web/API/Window/onunhandledrejection /en-US/docs/Web/API/Window/unhandledrejection_event /en-US/docs/Web/API/Window/onunload /en-US/docs/Web/API/Window/unload_event /en-US/docs/Web/API/Window/onuserproximity /en-US/docs/Web/API/UserProximityEvent /en-US/docs/Web/API/Window/onvrdisplayactivate /en-US/docs/Web/API/Window/vrdisplayactivate_event @@ -9780,7 +9780,9 @@ /en-US/docs/Web/API/WindowEventHandlers/onmessage /en-US/docs/Web/API/Window/message_event /en-US/docs/Web/API/WindowEventHandlers/onmessageerror /en-US/docs/Web/API/Window/messageerror_event /en-US/docs/Web/API/WindowEventHandlers/onpopstate /en-US/docs/Web/API/Window/popstate_event +/en-US/docs/Web/API/WindowEventHandlers/onrejectionhandled /en-US/docs/Web/API/Window/rejectionhandled_event /en-US/docs/Web/API/WindowEventHandlers/onstorage /en-US/docs/Web/API/Window/storage_event +/en-US/docs/Web/API/WindowEventHandlers/onunhandledrejection /en-US/docs/Web/API/Window/unhandledrejection_event /en-US/docs/Web/API/WindowEventHandlers/onunload /en-US/docs/Web/API/Window/unload_event /en-US/docs/Web/API/WindowEventHandlers/rejectionhandled_event /en-US/docs/Web/API/Window/rejectionhandled_event /en-US/docs/Web/API/WindowEventHandlers/unhandledrejection_event /en-US/docs/Web/API/Window/unhandledrejection_event diff --git a/files/en-us/_wikihistory.json b/files/en-us/_wikihistory.json index 37743499ef91dcc..b9c838eeff57130 100644 --- a/files/en-us/_wikihistory.json +++ b/files/en-us/_wikihistory.json @@ -85247,29 +85247,6 @@ "Ehsan" ] }, - "Web/API/WindowEventHandlers/onrejectionhandled": { - "modified": "2020-10-15T21:38:13.769Z", - "contributors": [ - "mfluehr", - "fscholz", - "Sheppy", - "chrisdavidmills", - "rolfedh", - "jpmedley" - ] - }, - "Web/API/WindowEventHandlers/onunhandledrejection": { - "modified": "2020-10-15T21:38:15.201Z", - "contributors": [ - "mfluehr", - "fscholz", - "Sheppy", - "chrisdavidmills", - "teoli", - "rolfedh", - "jpmedley" - ] - }, "Web/API/Worker": { "modified": "2020-10-15T21:10:11.615Z", "contributors": [ diff --git a/files/en-us/web/api/promiserejectionevent/promise/index.md b/files/en-us/web/api/promiserejectionevent/promise/index.md index 080577be28e29b2..e897e22d1483f45 100644 --- a/files/en-us/web/api/promiserejectionevent/promise/index.md +++ b/files/en-us/web/api/promiserejectionevent/promise/index.md @@ -63,5 +63,5 @@ window.onunhandledrejection = function(event) { rejection events")}} - {{jsxref("Promise")}} - {{domxref("PromiseRejectionEvent")}} -- {{event("rejectionhandled")}} +- {{domxref("Window.rejectionhandled_event", "rejectionhandled")}} - {{domxref("Window.unhandledrejection_event", "unhandledrejection")}} diff --git a/files/en-us/web/api/promiserejectionevent/promiserejectionevent/index.md b/files/en-us/web/api/promiserejectionevent/promiserejectionevent/index.md index ae59615332c7222..047aecb18b59fe1 100644 --- a/files/en-us/web/api/promiserejectionevent/promiserejectionevent/index.md +++ b/files/en-us/web/api/promiserejectionevent/promiserejectionevent/index.md @@ -24,7 +24,7 @@ for errors. There are two types of `PromiseRejectionEvent`: {{domxref("Window.unhandledrejection_event", "unhandledrejection")}} is sent by the JavaScript runtime when a promise is -rejected but the rejection goes unhandled. A {{event("rejectionhandled")}} event is +rejected but the rejection goes unhandled. A {{domxref("Window.rejectionhandled_event", "rejectionhandled")}}event is emitted if a promise is rejected but the rejection is caught by a rejection handler. ## Syntax @@ -41,8 +41,7 @@ _The `PromiseRejectionEvent()` constructor also inherits parameters from - `type` - : A string representing the name of the type of the `PromiseRejectionEvent`. This is case-sensitive and should be one of - {{event("rejectionhandled", '"rejectionhandled"')}} or {{event("unhandledrejection", - '"unhandledrejection"')}}, to match the event names of the possible (non-synthetic) + {{domxref("Window.rejectionhandled_event", "rejectionhandled")}} or {{domxref("Window.unhandledrejection_event", "unhandledrejection")}}, to match the event names of the possible (non-synthetic) {{domxref("PromiseRejectionEvent")}} events that user agents can actually fire). - `options` diff --git a/files/en-us/web/api/promiserejectionevent/reason/index.md b/files/en-us/web/api/promiserejectionevent/reason/index.md index 8ba857611f0148e..9478502e0fefd8f 100644 --- a/files/en-us/web/api/promiserejectionevent/reason/index.md +++ b/files/en-us/web/api/promiserejectionevent/reason/index.md @@ -46,5 +46,5 @@ window.onunhandledrejection = function(e) { - {{jsxref("Promise")}} - {{domxref("PromiseRejectionEvent")}} -- {{event("rejectionhandled")}} +- {{domxref("Window.rejectionhandled_event", "rejectionhandled")}} - {{domxref("Window.unhandledrejection_event", "unhandledrejection")}} diff --git a/files/en-us/web/api/window/index.md b/files/en-us/web/api/window/index.md index 7e3dfb86661b084..d8dbc7df6dfd665 100644 --- a/files/en-us/web/api/window/index.md +++ b/files/en-us/web/api/window/index.md @@ -309,8 +309,6 @@ These are properties of the window object that can be set to establish event han _This interface inherits event handlers from the {{domxref("EventTarget")}} interface and implements event handlers from {{domxref("WindowEventHandlers")}}._ -- {{domxref("WindowEventHandlers.onrejectionhandled")}} - - : An event handler for handled {{jsxref("Promise")}} rejection events. - {{domxref("Window.onuserproximity")}} {{deprecated_inline}} - : An event handler property for user proximity events (see {{domxref("UserProximityEvent")}}). @@ -368,8 +366,6 @@ _This interface inherits event handlers from the {{domxref("EventTarget")}} inte - : Is an [event handler](/en-US/docs/Web/Events/Event_handlers) representing the code to be called when the {{event("selectionchange")}} event is raised. - {{domxref("GlobalEventHandlers.onsubmit")}} - : Called when a form is submitted -- {{domxref("WindowEventHandlers.onunhandledrejection")}} {{experimental_inline}} - - : An event handler for unhandled {{jsxref("Promise")}} rejection events. ## Events @@ -504,10 +500,8 @@ Listen to these events using [`addEventListener()`](/en-US/docs/Web/API/EventTar - {{domxref("Window/rejectionhandled_event", "rejectionhandled")}} - : Sent every time a JavaScript {{jsxref("Promise")}} is rejected, regardless of whether or not there is a handler in place to catch the rejection. - Also available through the {{domxref("WindowEventHandlers/onrejectionhandled", "onrejectionhandled")}} event handler property. - {{domxref("Window/unhandledrejection_event", "unhandledrejection")}} - : Sent when a JavaScript {{jsxref("Promise")}} is rejected but there is no handler in place to catch the rejection. - Also available using the {{domxref("WindowEventHandlers/onunhandledrejection", "onunhandledrejection")}} event handler property. ### Transition events diff --git a/files/en-us/web/api/window/rejectionhandled_event/index.md b/files/en-us/web/api/window/rejectionhandled_event/index.md index 2195c53f51c674e..02f47f2f217d870 100644 --- a/files/en-us/web/api/window/rejectionhandled_event/index.md +++ b/files/en-us/web/api/window/rejectionhandled_event/index.md @@ -23,28 +23,35 @@ The **`rejectionhandled`** event is sent to the script's global scope (usually { This can be used in debugging and for general application resiliency, in tandem with the {{domxref("Window.unhandledrejection_event", "unhandledrejection")}} event, which is sent when a promise is rejected but there is no handler for the rejection. - - - - - - - - - - - - - - - - - - - -
BubblesNo
CancelableNo
Interface{{domxref("PromiseRejectionEvent")}}
Event handler property - {{domxref("WindowEventHandlers.onrejectionhandled", "onrejectionhandled")}} -
+## Syntax + +Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property. + +```js +addEventListener('rejectionhandled', event => { }); +onrejectionhandled = event => { }; +``` + +## Event type + +A {{domxref("PromiseRejectionEvent")}}. Inherits from {{domxref("Event")}}. + +{{InheritanceDiagram("PromiseRejectionEvent")}} + +## Event properties + +- {{domxref("PromiseRejectionEvent.promise")}} {{readonlyInline}} + - : The JavaScript {{jsxref("Promise")}} that was rejected. +- {{domxref("PromiseRejectionEvent.reason")}} {{readOnlyInline}} + - : A value or {{jsxref("Object")}} indicating why the promise was rejected, as passed to {{jsxref("Promise.reject()")}}. + +## Event handler aliases + +In addition to the `Window` interface, the event handler property `onrejectionhandled` is also available on the following targets: + +- {{domxref("HTMLBodyElement")}} +- {{domxref("HTMLFrameSetElement")}} +- {{domxref("SVGSVGElement")}} ## Example diff --git a/files/en-us/web/api/window/unhandledrejection_event/index.md b/files/en-us/web/api/window/unhandledrejection_event/index.md index 0d17543938936bf..1727eadf547a105 100644 --- a/files/en-us/web/api/window/unhandledrejection_event/index.md +++ b/files/en-us/web/api/window/unhandledrejection_event/index.md @@ -23,28 +23,35 @@ The **`unhandledrejection`** event is sent to the global scope of a script when This is useful for debugging and for providing fallback error handling for unexpected situations. - - - - - - - - - - - - - - - - - - - -
BubblesNo
CancelableYes
Interface{{domxref("PromiseRejectionEvent")}}
Event handler property - {{domxref("WindowEventHandlers.onunhandledrejection", "onunhandledrejection")}} -
+## Syntax + +Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property. + +```js +addEventListener('unhandledrejection', event => { }); +onunhandledrejection = event => { }; +``` + +## Event type + +A {{domxref("PromiseRejectionEvent")}}. Inherits from {{domxref("Event")}}. + +{{InheritanceDiagram("PromiseRejectionEvent")}} + +## Event properties + +- {{domxref("PromiseRejectionEvent.promise")}} {{readonlyInline}} + - : The JavaScript {{jsxref("Promise")}} that was rejected. +- {{domxref("PromiseRejectionEvent.reason")}} {{readOnlyInline}} + - : A value or {{jsxref("Object")}} indicating why the promise was rejected, as passed to {{jsxref("Promise.reject()")}}. + +## Event handler aliases + +In addition to the `Window` interface, the event handler property `onunhandledrejection` is also available on the following targets: + +- {{domxref("HTMLBodyElement")}} +- {{domxref("HTMLFrameSetElement")}} +- {{domxref("SVGSVGElement")}} ## Usage notes @@ -52,13 +59,6 @@ Allowing the `unhandledrejection` event to bubble will eventually result in an e ## Examples -Here we have a few examples showing ways you can make use of the `unhandledrejection` event. The event includes two useful pieces of information: - -- `promise` - - : The actual {{jsxref("Promise")}} which was rejected with no handler available to deal with the rejection. -- `reason` - - : The reason that would have been passed into the rejection handler if one had existed. See {{jsxref("Promise.catch", "catch()")}} for details. - ### Basic error logging This example logs information about the unhandled promise rejection to the console. @@ -69,7 +69,7 @@ window.addEventListener("unhandledrejection", event => { }); ``` -You can also use the {{domxref("WindowEventHandlers.onunhandledrejection", "onunhandledrejection")}} event handler property to set up the event listener: +You can also use the `onunhandledrejection` event handler property to set up the event listener: ```js window.onunhandledrejection = event => { @@ -103,6 +103,5 @@ window.addEventListener('unhandledrejection', function (event) { ## See also - {{SectionOnPage("/en-US/docs/Web/JavaScript/Guide/Using_promises", "Promise rejection events")}} -- {{domxref("WindowEventHandlers.onunhandledrejection", "onunhandledrejection")}} event handler property - {{domxref("Window/rejectionhandled_event", "rejectionhandled")}} event - {{jsxref("Promise")}} diff --git a/files/en-us/web/api/windoweventhandlers/index.md b/files/en-us/web/api/windoweventhandlers/index.md index 3b6d4862a984806..933c3860d343945 100644 --- a/files/en-us/web/api/windoweventhandlers/index.md +++ b/files/en-us/web/api/windoweventhandlers/index.md @@ -20,11 +20,6 @@ The **`WindowEventHandlers`** mixin describes the event handlers common to sever _The events properties, of the form `onXYZ`, are defined on the {{domxref("WindowEventHandlers")}}, and implemented by {{domxref("Window")}}, and {{domxref("WorkerGlobalScope")}} for Web Workers._ -- {{domxref("WindowEventHandlers.onrejectionhandled")}} - - : Is an [event handler](/en-US/docs/Web/Events/Event_handlers) representing the code to be called when the {{event("rejectionhandled")}} event is raised, indicating that a {{jsxref("Promise")}} was rejected and the rejection has been handled. -- {{domxref("WindowEventHandlers.onunhandledrejection")}} - - : Is an [event handler](/en-US/docs/Web/Events/Event_handlers) representing the code to be called when the {{domxref("Window.unhandledrejection_event", "unhandledrejection")}} event is raised, indicating that a {{jsxref("Promise")}} was rejected but the rejection was not handled. - ## Methods _This interface defines no method._ diff --git a/files/en-us/web/api/windoweventhandlers/onrejectionhandled/index.md b/files/en-us/web/api/windoweventhandlers/onrejectionhandled/index.md deleted file mode 100644 index d88d5fd666f3c63..000000000000000 --- a/files/en-us/web/api/windoweventhandlers/onrejectionhandled/index.md +++ /dev/null @@ -1,42 +0,0 @@ ---- -title: WindowEventHandlers.onrejectionhandled -slug: Web/API/WindowEventHandlers/onrejectionhandled -tags: - - API - - Event Handler - - HTML DOM - - JavaScript - - Promises - - Property - - Reference - - WindowEventHandlers - - events - - onrejectionhandled -browser-compat: api.WindowEventHandlers.onrejectionhandled ---- -{{APIRef}} - -The **`onrejectionhandled`** property of the -{{domxref("WindowEventHandlers")}} mixin is the [event handler](/en-US/docs/Web/Events/Event_handlers) for -processing {{event("rejectionhandled")}} events. These events are raised when -{{jsxref("Promise")}}s are rejected. - -## Value - -An [event handler](/en-US/docs/Web/Events/Event_handlers). - -## Examples - -```js -window.onrejectionhandled = function(e) { - console.log(e.reason); -} -``` - -## Specifications - -{{Specifications}} - -## Browser compatibility - -{{Compat}} diff --git a/files/en-us/web/api/windoweventhandlers/onunhandledrejection/index.md b/files/en-us/web/api/windoweventhandlers/onunhandledrejection/index.md deleted file mode 100644 index 67023fea40fa090..000000000000000 --- a/files/en-us/web/api/windoweventhandlers/onunhandledrejection/index.md +++ /dev/null @@ -1,43 +0,0 @@ ---- -title: WindowEventHandlers.onunhandledrejection -slug: Web/API/WindowEventHandlers/onunhandledrejection -tags: - - API - - Event Handler - - HTML DOM - - Promises - - Property - - Reference - - WindowEventHandlers - - events - - onunhandledrejection -browser-compat: api.WindowEventHandlers.onunhandledrejection ---- -{{APIRef}} - -The **`onunhandledrejection`** property of the -{{domxref("WindowEventHandlers")}} mixin is the [event handler](/en-US/docs/Web/Events/Event_handlers) for -processing {{domxref("Window.unhandledrejection_event", "unhandledrejection")}} events. These events are raised for unhandled -{{jsxref("Promise")}} rejections. - -## Value - -An [event handler](/en-US/docs/Web/Events/Event_handlers) or function to call when `unhandledrejection` events are received by the window. The event handler receives as an input parameter as a {{domxref("PromiseRejectionEvent")}}. - -## Examples - -This example logs unhandled rejections' `reason` values to the console. - -```js -window.onunhandledrejection = function(e) { - console.log(e.reason); -} -``` - -## Specifications - -{{Specifications}} - -## Browser compatibility - -{{Compat}}