```js
+ const { on, EventEmitter } = require('events');
+
+
(async () => {
+ const ee = new EventEmitter();
+
// Emit later on
+ process.nextTick(() => {
+ ee.emit('foo', 'bar');
+ ee.emit('foo', 42);
+ });
+
for await (const event of on(ee, 'foo')) {
+ // The execution of this inner block is synchronous and it
+ // processes one event at a time (even with await). Do not use
+ // if concurrent execution is required.
+ console.log(event); // prints ['bar'] [42]
+ }
+ // Unreachable here
+ })();
+
+Returnsan`AsyncIterator`thatiterates`eventName`events. Itwillthrow
+ifthe`EventEmitter`emits`'error'`. Itremovesalllistenerswhen
+exitingtheloop. The`value`returnedbyeachiterationisanarray
+composedoftheemittedeventarguments.
+
+An`AbortSignal`canbeusedtocancelwaitingon events:
+
+```js
+const { on, EventEmitter } = require('events');
+const ac = new AbortController();
+
+(async () => {
+ const ee = new EventEmitter();
+
+ // Emit later on
+ process.nextTick(() => {
+ ee.emit('foo', 'bar');
+ ee.emit('foo', 42);
+ });
+
+ for await (const event of on(ee, 'foo', { signal: ac.signal })) {
+ // The execution of this inner block is synchronous and it
+ // processes one event at a time (even with await). Do not use
+ // if concurrent execution is required.
+ console.log(event); // prints ['bar'] [42]
+ }
+ // Unreachable here
+})();
+
+process.nextTick(() => ac.abort());
+
+
+
since
+
v13.6.0, v12.16.0
+
+
+
Parameters
@@ -2100,12 +2201,16 @@
eventName: string
+
+
The name of the event being listened for
+
Optional options: StaticEventEmitterOptions
Returns AsyncIterableIterator<any>
+
that iterates eventName events emitted by the emitter
@@ -2121,9 +2226,88 @@
+
+
+
Creates a Promise that is fulfilled when the EventEmitter emits the given
+ event or that is rejected if the EventEmitter emits 'error' while waiting.
+ The Promise will resolve with an array of all the arguments emitted to the
+ given event.
+
+
This method is intentionally generic and works with the web platformEventTarget interface, which has no special'error' event
+ semantics and does not listen to the 'error' event.
The special handling of the 'error' event is only used when events.once()is used to wait for another event. If events.once() is used to wait for the
+ 'error' event itself, then it is treated as any other kind of event without
+ special handling:
_config
-_options
-Returns Promise<void>
@@ -818,7 +818,7 @@Returns undefined | boolean
@@ -1179,7 +1179,7 @@Static getEventListener
+ +Static getEventListeners
-- get
EventListener(emitter: DOMEventTarget | EventEmitter, name: string | symbol): Function[]
+ - get
EventListeners(emitter: DOMEventTarget | EventEmitter, name: string | symbol): Function[]
Returns a list listener for a specific emitter event name.
+Returns a copy of the array of listeners for the event named
eventName
.For
+EventEmitter
s this behaves exactly the same as calling.listeners
on + the emitter.For
+ +EventTarget
s this is the only way to get the event listeners for the + event target. This is useful for debugging and diagnostic purposes.+- since
+
+
v15.2.0
+Parameters
@@ -2056,13 +2080,26 @@
+
+
+
@@ -2070,9 +2107,15 @@
-
+
-
+
+
+
+
-
@@ -2100,12 +2201,16 @@
-
+
-
A class method that returns the number of listeners for the given
+eventName
registered on the givenemitter
.+- since
+
- deprecated
-
v0.9.12
+since v4.0.0
+Since v3.2.0 - Use
listenerCount
instead.emitter: EventEmitter
+The emitter to query
+eventName: string | symbol
+The event name
+Returns number
@@ -2090,9 +2133,67 @@ +```js + const { on, EventEmitter } = require('events');
+(async () => { + const ee = new EventEmitter();
+// Emit later on + process.nextTick(() => { + ee.emit('foo', 'bar'); + ee.emit('foo', 42); + });
+for await (const event of on(ee, 'foo')) { + // The execution of this inner block is synchronous and it + // processes one event at a time (even with await). Do not use + // if concurrent execution is required. + console.log(event); // prints ['bar'] [42] + } + // Unreachable here + })();
+ ++- since
+
+
+v13.6.0, v12.16.0
+Parameters
eventName: string
+The name of the event being listened for
+Optional options: StaticEventEmitterOptions
Returns AsyncIterableIterator<any>
+that iterates
eventName
events emitted by theemitter
Creates a
+Promise
that is fulfilled when theEventEmitter
emits the given + event or that is rejected if theEventEmitter
emits'error'
while waiting. + ThePromise
will resolve with an array of all the arguments emitted to the + given event.This method is intentionally generic and works with the web platformEventTarget interface, which has no special
+ +'error'
event + semantics and does not listen to the'error'
event.The special handling of the
+ +'error'
event is only used whenevents.once()
is used to wait for another event. Ifevents.once()
is used to wait for the + 'error'
event itself, then it is treated as any other kind of event without + special handling:An
+ +AbortSignal
can be used to cancel waiting for the event:+- since
+
+
+v11.13.0, v10.16.0
+Parameters
Parameters
@@ -2371,7 +2555,7 @@ createlibp2p - v0.32.4
+libp2p - v0.31.8
libp2p - v0.32.4
+libp2p - v0.31.8