Skip to content

Commit

Permalink
Fix promise rejection events (#15244)
Browse files Browse the repository at this point in the history
  • Loading branch information
Elchi3 authored Apr 22, 2022
1 parent 6f0ba51 commit bf6ace9
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 179 deletions.
6 changes: 4 additions & 2 deletions files/en-us/_redirects.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
23 changes: 0 additions & 23 deletions files/en-us/_wikihistory.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
2 changes: 1 addition & 1 deletion files/en-us/web/api/promiserejectionevent/promise/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")}}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`

Expand Down
2 changes: 1 addition & 1 deletion files/en-us/web/api/promiserejectionevent/reason/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ window.onunhandledrejection = function(e) {

- {{jsxref("Promise")}}
- {{domxref("PromiseRejectionEvent")}}
- {{event("rejectionhandled")}}
- {{domxref("Window.rejectionhandled_event", "rejectionhandled")}}
- {{domxref("Window.unhandledrejection_event", "unhandledrejection")}}
6 changes: 0 additions & 6 deletions files/en-us/web/api/window/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")}}).

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
51 changes: 29 additions & 22 deletions files/en-us/web/api/window/rejectionhandled_event/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<table class="properties">
<tbody>
<tr>
<th scope="row">Bubbles</th>
<td>No</td>
</tr>
<tr>
<th scope="row">Cancelable</th>
<td>No</td>
</tr>
<tr>
<th scope="row">Interface</th>
<td>{{domxref("PromiseRejectionEvent")}}</td>
</tr>
<tr>
<th scope="row">Event handler property</th>
<td>
{{domxref("WindowEventHandlers.onrejectionhandled", "onrejectionhandled")}}
</td>
</tr>
</tbody>
</table>
## 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

Expand Down
61 changes: 30 additions & 31 deletions files/en-us/web/api/window/unhandledrejection_event/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,42 @@ 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.

<table class="properties">
<tbody>
<tr>
<th scope="row">Bubbles</th>
<td>No</td>
</tr>
<tr>
<th scope="row">Cancelable</th>
<td>Yes</td>
</tr>
<tr>
<th scope="row">Interface</th>
<td>{{domxref("PromiseRejectionEvent")}}</td>
</tr>
<tr>
<th scope="row">Event handler property</th>
<td>
{{domxref("WindowEventHandlers.onunhandledrejection", "onunhandledrejection")}}
</td>
</tr>
</tbody>
</table>
## 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

Allowing the `unhandledrejection` event to bubble will eventually result in an error message being output to the console. You can prevent this by calling {{domxref("Event.preventDefault", "preventDefault()")}} on the {{domxref("PromiseRejectionEvent")}}; see [Preventing default handling](#preventing_default_handling) below for an example.

## 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.
Expand All @@ -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 => {
Expand Down Expand Up @@ -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")}}
5 changes: 0 additions & 5 deletions files/en-us/web/api/windoweventhandlers/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit bf6ace9

Please sign in to comment.