Skip to content

Commit a9f0fc9

Browse files
jasnelladdaleax
authored andcommitted
doc: document behavior for once(ee, 'error')
Fixes: #31244 PR-URL: #34225 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent ed055c0 commit a9f0fc9

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

doc/api/events.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ added: v11.13.0
829829
* Returns: {Promise}
830830

831831
Creates a `Promise` that is fulfilled when the `EventEmitter` emits the given
832-
event or that is rejected when the `EventEmitter` emits `'error'`.
832+
event or that is rejected if the `EventEmitter` emits `'error'` while waiting.
833833
The `Promise` will resolve with an array of all the arguments emitted to the
834834
given event.
835835

@@ -865,7 +865,26 @@ async function run() {
865865
run();
866866
```
867867

868-
## events.captureRejections
868+
The special handling of the `'error'` event is only used when `events.once()`
869+
is used to wait for another event. If `events.once()` is used to wait for the
870+
'`error'` event itself, then it is treated as any other kind of event without
871+
special handling:
872+
873+
```js
874+
const { EventEmitter, once } = require('events');
875+
876+
const ee = new EventEmitter();
877+
878+
once(ee, 'error')
879+
.then(([err]) => console.log('ok', err.message))
880+
.catch((err) => console.log('error', err.message));
881+
882+
ee.emit('error', new Error('boom'));
883+
884+
// Prints: ok boom
885+
```
886+
887+
## `events.captureRejections`
869888
<!-- YAML
870889
added: v12.16.0
871890
-->

0 commit comments

Comments
 (0)