M
events.on
History
Added in: v13.6.0, v12.16.0
v13.6.0, v12.16.0
Introduced in: v0.10.0
v0.10.0
Support
highWaterMark and lowWaterMark options, For consistency. Old options are still supported.v22.0.0, v20.13.0
The
close, highWatermark, and lowWatermark options are supported now.v20.0.0
events.on(emitter, eventName, options?): AsyncIterator
Attributes
emitter:
EventEmitteroptions:
Objectsignal:
AbortSignalCan be used to cancel awaiting events.
close:
string[]Names of events that will end the iteration.
highWaterMark?:
integerDefault:
Number.MAX_SAFE_INTEGER
The high watermark. The emitter is paused every time the size of events
being buffered is higher than it. Supported only on emitters implementing
pause() and resume() methods.lowWaterMark?:
integerDefault:
1
The low watermark. The emitter is resumed every time the size of events
being buffered is lower than it. Supported only on emitters implementing
pause() and resume() methods.Returns:
AsyncIteratorthat iterates
eventName events emitted by the emitterimport { on, EventEmitter } from 'node:events'; import process from 'node:process'; 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
const { on, EventEmitter } = require('node: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 })();
Returns an AsyncIterator that iterates eventName events. It will throw
if the EventEmitter emits 'error'. It removes all listeners when
exiting the loop. The value returned by each iteration is an array
composed of the emitted event arguments.
An AbortSignal can be used to cancel waiting on events:
import { on, EventEmitter } from 'node:events'; import process from 'node:process'; 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());
const { on, EventEmitter } = require('node: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());