xref: /third_party/node/doc/api/events.md (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci# Events
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci<!--introduced_in=v0.10.0-->
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci> Stability: 2 - Stable
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci<!--type=module-->
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci<!-- source_link=lib/events.js -->
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciMuch of the Node.js core API is built around an idiomatic asynchronous
121cb0ef41Sopenharmony_cievent-driven architecture in which certain kinds of objects (called "emitters")
131cb0ef41Sopenharmony_ciemit named events that cause `Function` objects ("listeners") to be called.
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciFor instance: a [`net.Server`][] object emits an event each time a peer
161cb0ef41Sopenharmony_ciconnects to it; a [`fs.ReadStream`][] emits an event when the file is opened;
171cb0ef41Sopenharmony_cia [stream][] emits an event whenever data is available to be read.
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciAll objects that emit events are instances of the `EventEmitter` class. These
201cb0ef41Sopenharmony_ciobjects expose an `eventEmitter.on()` function that allows one or more
211cb0ef41Sopenharmony_cifunctions to be attached to named events emitted by the object. Typically,
221cb0ef41Sopenharmony_cievent names are camel-cased strings but any valid JavaScript property key
231cb0ef41Sopenharmony_cican be used.
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciWhen the `EventEmitter` object emits an event, all of the functions attached
261cb0ef41Sopenharmony_cito that specific event are called _synchronously_. Any values returned by the
271cb0ef41Sopenharmony_cicalled listeners are _ignored_ and discarded.
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciThe following example shows a simple `EventEmitter` instance with a single
301cb0ef41Sopenharmony_cilistener. The `eventEmitter.on()` method is used to register listeners, while
311cb0ef41Sopenharmony_cithe `eventEmitter.emit()` method is used to trigger the event.
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci```mjs
341cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciclass MyEmitter extends EventEmitter {}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciconst myEmitter = new MyEmitter();
391cb0ef41Sopenharmony_cimyEmitter.on('event', () => {
401cb0ef41Sopenharmony_ci  console.log('an event occurred!');
411cb0ef41Sopenharmony_ci});
421cb0ef41Sopenharmony_cimyEmitter.emit('event');
431cb0ef41Sopenharmony_ci```
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci```cjs
461cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ciclass MyEmitter extends EventEmitter {}
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ciconst myEmitter = new MyEmitter();
511cb0ef41Sopenharmony_cimyEmitter.on('event', () => {
521cb0ef41Sopenharmony_ci  console.log('an event occurred!');
531cb0ef41Sopenharmony_ci});
541cb0ef41Sopenharmony_cimyEmitter.emit('event');
551cb0ef41Sopenharmony_ci```
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci## Passing arguments and `this` to listeners
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ciThe `eventEmitter.emit()` method allows an arbitrary set of arguments to be
601cb0ef41Sopenharmony_cipassed to the listener functions. Keep in mind that when
611cb0ef41Sopenharmony_cian ordinary listener function is called, the standard `this` keyword
621cb0ef41Sopenharmony_ciis intentionally set to reference the `EventEmitter` instance to which the
631cb0ef41Sopenharmony_cilistener is attached.
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci```mjs
661cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
671cb0ef41Sopenharmony_ciclass MyEmitter extends EventEmitter {}
681cb0ef41Sopenharmony_ciconst myEmitter = new MyEmitter();
691cb0ef41Sopenharmony_cimyEmitter.on('event', function(a, b) {
701cb0ef41Sopenharmony_ci  console.log(a, b, this, this === myEmitter);
711cb0ef41Sopenharmony_ci  // Prints:
721cb0ef41Sopenharmony_ci  //   a b MyEmitter {
731cb0ef41Sopenharmony_ci  //     _events: [Object: null prototype] { event: [Function (anonymous)] },
741cb0ef41Sopenharmony_ci  //     _eventsCount: 1,
751cb0ef41Sopenharmony_ci  //     _maxListeners: undefined,
761cb0ef41Sopenharmony_ci  //     [Symbol(kCapture)]: false
771cb0ef41Sopenharmony_ci  //   } true
781cb0ef41Sopenharmony_ci});
791cb0ef41Sopenharmony_cimyEmitter.emit('event', 'a', 'b');
801cb0ef41Sopenharmony_ci```
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci```cjs
831cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
841cb0ef41Sopenharmony_ciclass MyEmitter extends EventEmitter {}
851cb0ef41Sopenharmony_ciconst myEmitter = new MyEmitter();
861cb0ef41Sopenharmony_cimyEmitter.on('event', function(a, b) {
871cb0ef41Sopenharmony_ci  console.log(a, b, this, this === myEmitter);
881cb0ef41Sopenharmony_ci  // Prints:
891cb0ef41Sopenharmony_ci  //   a b MyEmitter {
901cb0ef41Sopenharmony_ci  //     _events: [Object: null prototype] { event: [Function (anonymous)] },
911cb0ef41Sopenharmony_ci  //     _eventsCount: 1,
921cb0ef41Sopenharmony_ci  //     _maxListeners: undefined,
931cb0ef41Sopenharmony_ci  //     [Symbol(kCapture)]: false
941cb0ef41Sopenharmony_ci  //   } true
951cb0ef41Sopenharmony_ci});
961cb0ef41Sopenharmony_cimyEmitter.emit('event', 'a', 'b');
971cb0ef41Sopenharmony_ci```
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ciIt is possible to use ES6 Arrow Functions as listeners, however, when doing so,
1001cb0ef41Sopenharmony_cithe `this` keyword will no longer reference the `EventEmitter` instance:
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci```mjs
1031cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
1041cb0ef41Sopenharmony_ciclass MyEmitter extends EventEmitter {}
1051cb0ef41Sopenharmony_ciconst myEmitter = new MyEmitter();
1061cb0ef41Sopenharmony_cimyEmitter.on('event', (a, b) => {
1071cb0ef41Sopenharmony_ci  console.log(a, b, this);
1081cb0ef41Sopenharmony_ci  // Prints: a b {}
1091cb0ef41Sopenharmony_ci});
1101cb0ef41Sopenharmony_cimyEmitter.emit('event', 'a', 'b');
1111cb0ef41Sopenharmony_ci```
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci```cjs
1141cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
1151cb0ef41Sopenharmony_ciclass MyEmitter extends EventEmitter {}
1161cb0ef41Sopenharmony_ciconst myEmitter = new MyEmitter();
1171cb0ef41Sopenharmony_cimyEmitter.on('event', (a, b) => {
1181cb0ef41Sopenharmony_ci  console.log(a, b, this);
1191cb0ef41Sopenharmony_ci  // Prints: a b {}
1201cb0ef41Sopenharmony_ci});
1211cb0ef41Sopenharmony_cimyEmitter.emit('event', 'a', 'b');
1221cb0ef41Sopenharmony_ci```
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci## Asynchronous vs. synchronous
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ciThe `EventEmitter` calls all listeners synchronously in the order in which
1271cb0ef41Sopenharmony_cithey were registered. This ensures the proper sequencing of
1281cb0ef41Sopenharmony_cievents and helps avoid race conditions and logic errors. When appropriate,
1291cb0ef41Sopenharmony_cilistener functions can switch to an asynchronous mode of operation using
1301cb0ef41Sopenharmony_cithe `setImmediate()` or `process.nextTick()` methods:
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci```mjs
1331cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
1341cb0ef41Sopenharmony_ciclass MyEmitter extends EventEmitter {}
1351cb0ef41Sopenharmony_ciconst myEmitter = new MyEmitter();
1361cb0ef41Sopenharmony_cimyEmitter.on('event', (a, b) => {
1371cb0ef41Sopenharmony_ci  setImmediate(() => {
1381cb0ef41Sopenharmony_ci    console.log('this happens asynchronously');
1391cb0ef41Sopenharmony_ci  });
1401cb0ef41Sopenharmony_ci});
1411cb0ef41Sopenharmony_cimyEmitter.emit('event', 'a', 'b');
1421cb0ef41Sopenharmony_ci```
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci```cjs
1451cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
1461cb0ef41Sopenharmony_ciclass MyEmitter extends EventEmitter {}
1471cb0ef41Sopenharmony_ciconst myEmitter = new MyEmitter();
1481cb0ef41Sopenharmony_cimyEmitter.on('event', (a, b) => {
1491cb0ef41Sopenharmony_ci  setImmediate(() => {
1501cb0ef41Sopenharmony_ci    console.log('this happens asynchronously');
1511cb0ef41Sopenharmony_ci  });
1521cb0ef41Sopenharmony_ci});
1531cb0ef41Sopenharmony_cimyEmitter.emit('event', 'a', 'b');
1541cb0ef41Sopenharmony_ci```
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci## Handling events only once
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ciWhen a listener is registered using the `eventEmitter.on()` method, that
1591cb0ef41Sopenharmony_cilistener is invoked _every time_ the named event is emitted.
1601cb0ef41Sopenharmony_ci
1611cb0ef41Sopenharmony_ci```mjs
1621cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
1631cb0ef41Sopenharmony_ciclass MyEmitter extends EventEmitter {}
1641cb0ef41Sopenharmony_ciconst myEmitter = new MyEmitter();
1651cb0ef41Sopenharmony_cilet m = 0;
1661cb0ef41Sopenharmony_cimyEmitter.on('event', () => {
1671cb0ef41Sopenharmony_ci  console.log(++m);
1681cb0ef41Sopenharmony_ci});
1691cb0ef41Sopenharmony_cimyEmitter.emit('event');
1701cb0ef41Sopenharmony_ci// Prints: 1
1711cb0ef41Sopenharmony_cimyEmitter.emit('event');
1721cb0ef41Sopenharmony_ci// Prints: 2
1731cb0ef41Sopenharmony_ci```
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci```cjs
1761cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
1771cb0ef41Sopenharmony_ciclass MyEmitter extends EventEmitter {}
1781cb0ef41Sopenharmony_ciconst myEmitter = new MyEmitter();
1791cb0ef41Sopenharmony_cilet m = 0;
1801cb0ef41Sopenharmony_cimyEmitter.on('event', () => {
1811cb0ef41Sopenharmony_ci  console.log(++m);
1821cb0ef41Sopenharmony_ci});
1831cb0ef41Sopenharmony_cimyEmitter.emit('event');
1841cb0ef41Sopenharmony_ci// Prints: 1
1851cb0ef41Sopenharmony_cimyEmitter.emit('event');
1861cb0ef41Sopenharmony_ci// Prints: 2
1871cb0ef41Sopenharmony_ci```
1881cb0ef41Sopenharmony_ci
1891cb0ef41Sopenharmony_ciUsing the `eventEmitter.once()` method, it is possible to register a listener
1901cb0ef41Sopenharmony_cithat is called at most once for a particular event. Once the event is emitted,
1911cb0ef41Sopenharmony_cithe listener is unregistered and _then_ called.
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_ci```mjs
1941cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
1951cb0ef41Sopenharmony_ciclass MyEmitter extends EventEmitter {}
1961cb0ef41Sopenharmony_ciconst myEmitter = new MyEmitter();
1971cb0ef41Sopenharmony_cilet m = 0;
1981cb0ef41Sopenharmony_cimyEmitter.once('event', () => {
1991cb0ef41Sopenharmony_ci  console.log(++m);
2001cb0ef41Sopenharmony_ci});
2011cb0ef41Sopenharmony_cimyEmitter.emit('event');
2021cb0ef41Sopenharmony_ci// Prints: 1
2031cb0ef41Sopenharmony_cimyEmitter.emit('event');
2041cb0ef41Sopenharmony_ci// Ignored
2051cb0ef41Sopenharmony_ci```
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ci```cjs
2081cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
2091cb0ef41Sopenharmony_ciclass MyEmitter extends EventEmitter {}
2101cb0ef41Sopenharmony_ciconst myEmitter = new MyEmitter();
2111cb0ef41Sopenharmony_cilet m = 0;
2121cb0ef41Sopenharmony_cimyEmitter.once('event', () => {
2131cb0ef41Sopenharmony_ci  console.log(++m);
2141cb0ef41Sopenharmony_ci});
2151cb0ef41Sopenharmony_cimyEmitter.emit('event');
2161cb0ef41Sopenharmony_ci// Prints: 1
2171cb0ef41Sopenharmony_cimyEmitter.emit('event');
2181cb0ef41Sopenharmony_ci// Ignored
2191cb0ef41Sopenharmony_ci```
2201cb0ef41Sopenharmony_ci
2211cb0ef41Sopenharmony_ci## Error events
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_ciWhen an error occurs within an `EventEmitter` instance, the typical action is
2241cb0ef41Sopenharmony_cifor an `'error'` event to be emitted. These are treated as special cases
2251cb0ef41Sopenharmony_ciwithin Node.js.
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_ciIf an `EventEmitter` does _not_ have at least one listener registered for the
2281cb0ef41Sopenharmony_ci`'error'` event, and an `'error'` event is emitted, the error is thrown, a
2291cb0ef41Sopenharmony_cistack trace is printed, and the Node.js process exits.
2301cb0ef41Sopenharmony_ci
2311cb0ef41Sopenharmony_ci```mjs
2321cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
2331cb0ef41Sopenharmony_ciclass MyEmitter extends EventEmitter {}
2341cb0ef41Sopenharmony_ciconst myEmitter = new MyEmitter();
2351cb0ef41Sopenharmony_cimyEmitter.emit('error', new Error('whoops!'));
2361cb0ef41Sopenharmony_ci// Throws and crashes Node.js
2371cb0ef41Sopenharmony_ci```
2381cb0ef41Sopenharmony_ci
2391cb0ef41Sopenharmony_ci```cjs
2401cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
2411cb0ef41Sopenharmony_ciclass MyEmitter extends EventEmitter {}
2421cb0ef41Sopenharmony_ciconst myEmitter = new MyEmitter();
2431cb0ef41Sopenharmony_cimyEmitter.emit('error', new Error('whoops!'));
2441cb0ef41Sopenharmony_ci// Throws and crashes Node.js
2451cb0ef41Sopenharmony_ci```
2461cb0ef41Sopenharmony_ci
2471cb0ef41Sopenharmony_ciTo guard against crashing the Node.js process the [`domain`][] module can be
2481cb0ef41Sopenharmony_ciused. (Note, however, that the `node:domain` module is deprecated.)
2491cb0ef41Sopenharmony_ci
2501cb0ef41Sopenharmony_ciAs a best practice, listeners should always be added for the `'error'` events.
2511cb0ef41Sopenharmony_ci
2521cb0ef41Sopenharmony_ci```mjs
2531cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
2541cb0ef41Sopenharmony_ciclass MyEmitter extends EventEmitter {}
2551cb0ef41Sopenharmony_ciconst myEmitter = new MyEmitter();
2561cb0ef41Sopenharmony_cimyEmitter.on('error', (err) => {
2571cb0ef41Sopenharmony_ci  console.error('whoops! there was an error');
2581cb0ef41Sopenharmony_ci});
2591cb0ef41Sopenharmony_cimyEmitter.emit('error', new Error('whoops!'));
2601cb0ef41Sopenharmony_ci// Prints: whoops! there was an error
2611cb0ef41Sopenharmony_ci```
2621cb0ef41Sopenharmony_ci
2631cb0ef41Sopenharmony_ci```cjs
2641cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
2651cb0ef41Sopenharmony_ciclass MyEmitter extends EventEmitter {}
2661cb0ef41Sopenharmony_ciconst myEmitter = new MyEmitter();
2671cb0ef41Sopenharmony_cimyEmitter.on('error', (err) => {
2681cb0ef41Sopenharmony_ci  console.error('whoops! there was an error');
2691cb0ef41Sopenharmony_ci});
2701cb0ef41Sopenharmony_cimyEmitter.emit('error', new Error('whoops!'));
2711cb0ef41Sopenharmony_ci// Prints: whoops! there was an error
2721cb0ef41Sopenharmony_ci```
2731cb0ef41Sopenharmony_ci
2741cb0ef41Sopenharmony_ciIt is possible to monitor `'error'` events without consuming the emitted error
2751cb0ef41Sopenharmony_ciby installing a listener using the symbol `events.errorMonitor`.
2761cb0ef41Sopenharmony_ci
2771cb0ef41Sopenharmony_ci```mjs
2781cb0ef41Sopenharmony_ciimport { EventEmitter, errorMonitor } from 'node:events';
2791cb0ef41Sopenharmony_ci
2801cb0ef41Sopenharmony_ciconst myEmitter = new EventEmitter();
2811cb0ef41Sopenharmony_cimyEmitter.on(errorMonitor, (err) => {
2821cb0ef41Sopenharmony_ci  MyMonitoringTool.log(err);
2831cb0ef41Sopenharmony_ci});
2841cb0ef41Sopenharmony_cimyEmitter.emit('error', new Error('whoops!'));
2851cb0ef41Sopenharmony_ci// Still throws and crashes Node.js
2861cb0ef41Sopenharmony_ci```
2871cb0ef41Sopenharmony_ci
2881cb0ef41Sopenharmony_ci```cjs
2891cb0ef41Sopenharmony_ciconst { EventEmitter, errorMonitor } = require('node:events');
2901cb0ef41Sopenharmony_ci
2911cb0ef41Sopenharmony_ciconst myEmitter = new EventEmitter();
2921cb0ef41Sopenharmony_cimyEmitter.on(errorMonitor, (err) => {
2931cb0ef41Sopenharmony_ci  MyMonitoringTool.log(err);
2941cb0ef41Sopenharmony_ci});
2951cb0ef41Sopenharmony_cimyEmitter.emit('error', new Error('whoops!'));
2961cb0ef41Sopenharmony_ci// Still throws and crashes Node.js
2971cb0ef41Sopenharmony_ci```
2981cb0ef41Sopenharmony_ci
2991cb0ef41Sopenharmony_ci## Capture rejections of promises
3001cb0ef41Sopenharmony_ci
3011cb0ef41Sopenharmony_ciUsing `async` functions with event handlers is problematic, because it
3021cb0ef41Sopenharmony_cican lead to an unhandled rejection in case of a thrown exception:
3031cb0ef41Sopenharmony_ci
3041cb0ef41Sopenharmony_ci```mjs
3051cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
3061cb0ef41Sopenharmony_ciconst ee = new EventEmitter();
3071cb0ef41Sopenharmony_ciee.on('something', async (value) => {
3081cb0ef41Sopenharmony_ci  throw new Error('kaboom');
3091cb0ef41Sopenharmony_ci});
3101cb0ef41Sopenharmony_ci```
3111cb0ef41Sopenharmony_ci
3121cb0ef41Sopenharmony_ci```cjs
3131cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
3141cb0ef41Sopenharmony_ciconst ee = new EventEmitter();
3151cb0ef41Sopenharmony_ciee.on('something', async (value) => {
3161cb0ef41Sopenharmony_ci  throw new Error('kaboom');
3171cb0ef41Sopenharmony_ci});
3181cb0ef41Sopenharmony_ci```
3191cb0ef41Sopenharmony_ci
3201cb0ef41Sopenharmony_ciThe `captureRejections` option in the `EventEmitter` constructor or the global
3211cb0ef41Sopenharmony_cisetting change this behavior, installing a `.then(undefined, handler)`
3221cb0ef41Sopenharmony_cihandler on the `Promise`. This handler routes the exception
3231cb0ef41Sopenharmony_ciasynchronously to the [`Symbol.for('nodejs.rejection')`][rejection] method
3241cb0ef41Sopenharmony_ciif there is one, or to [`'error'`][error] event handler if there is none.
3251cb0ef41Sopenharmony_ci
3261cb0ef41Sopenharmony_ci```mjs
3271cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
3281cb0ef41Sopenharmony_ciconst ee1 = new EventEmitter({ captureRejections: true });
3291cb0ef41Sopenharmony_ciee1.on('something', async (value) => {
3301cb0ef41Sopenharmony_ci  throw new Error('kaboom');
3311cb0ef41Sopenharmony_ci});
3321cb0ef41Sopenharmony_ci
3331cb0ef41Sopenharmony_ciee1.on('error', console.log);
3341cb0ef41Sopenharmony_ci
3351cb0ef41Sopenharmony_ciconst ee2 = new EventEmitter({ captureRejections: true });
3361cb0ef41Sopenharmony_ciee2.on('something', async (value) => {
3371cb0ef41Sopenharmony_ci  throw new Error('kaboom');
3381cb0ef41Sopenharmony_ci});
3391cb0ef41Sopenharmony_ci
3401cb0ef41Sopenharmony_ciee2[Symbol.for('nodejs.rejection')] = console.log;
3411cb0ef41Sopenharmony_ci```
3421cb0ef41Sopenharmony_ci
3431cb0ef41Sopenharmony_ci```cjs
3441cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
3451cb0ef41Sopenharmony_ciconst ee1 = new EventEmitter({ captureRejections: true });
3461cb0ef41Sopenharmony_ciee1.on('something', async (value) => {
3471cb0ef41Sopenharmony_ci  throw new Error('kaboom');
3481cb0ef41Sopenharmony_ci});
3491cb0ef41Sopenharmony_ci
3501cb0ef41Sopenharmony_ciee1.on('error', console.log);
3511cb0ef41Sopenharmony_ci
3521cb0ef41Sopenharmony_ciconst ee2 = new EventEmitter({ captureRejections: true });
3531cb0ef41Sopenharmony_ciee2.on('something', async (value) => {
3541cb0ef41Sopenharmony_ci  throw new Error('kaboom');
3551cb0ef41Sopenharmony_ci});
3561cb0ef41Sopenharmony_ci
3571cb0ef41Sopenharmony_ciee2[Symbol.for('nodejs.rejection')] = console.log;
3581cb0ef41Sopenharmony_ci```
3591cb0ef41Sopenharmony_ci
3601cb0ef41Sopenharmony_ciSetting `events.captureRejections = true` will change the default for all
3611cb0ef41Sopenharmony_cinew instances of `EventEmitter`.
3621cb0ef41Sopenharmony_ci
3631cb0ef41Sopenharmony_ci```mjs
3641cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
3651cb0ef41Sopenharmony_ci
3661cb0ef41Sopenharmony_ciEventEmitter.captureRejections = true;
3671cb0ef41Sopenharmony_ciconst ee1 = new EventEmitter();
3681cb0ef41Sopenharmony_ciee1.on('something', async (value) => {
3691cb0ef41Sopenharmony_ci  throw new Error('kaboom');
3701cb0ef41Sopenharmony_ci});
3711cb0ef41Sopenharmony_ci
3721cb0ef41Sopenharmony_ciee1.on('error', console.log);
3731cb0ef41Sopenharmony_ci```
3741cb0ef41Sopenharmony_ci
3751cb0ef41Sopenharmony_ci```cjs
3761cb0ef41Sopenharmony_ciconst events = require('node:events');
3771cb0ef41Sopenharmony_cievents.captureRejections = true;
3781cb0ef41Sopenharmony_ciconst ee1 = new events.EventEmitter();
3791cb0ef41Sopenharmony_ciee1.on('something', async (value) => {
3801cb0ef41Sopenharmony_ci  throw new Error('kaboom');
3811cb0ef41Sopenharmony_ci});
3821cb0ef41Sopenharmony_ci
3831cb0ef41Sopenharmony_ciee1.on('error', console.log);
3841cb0ef41Sopenharmony_ci```
3851cb0ef41Sopenharmony_ci
3861cb0ef41Sopenharmony_ciThe `'error'` events that are generated by the `captureRejections` behavior
3871cb0ef41Sopenharmony_cido not have a catch handler to avoid infinite error loops: the
3881cb0ef41Sopenharmony_cirecommendation is to **not use `async` functions as `'error'` event handlers**.
3891cb0ef41Sopenharmony_ci
3901cb0ef41Sopenharmony_ci## Class: `EventEmitter`
3911cb0ef41Sopenharmony_ci
3921cb0ef41Sopenharmony_ci<!-- YAML
3931cb0ef41Sopenharmony_ciadded: v0.1.26
3941cb0ef41Sopenharmony_cichanges:
3951cb0ef41Sopenharmony_ci  - version:
3961cb0ef41Sopenharmony_ci     - v13.4.0
3971cb0ef41Sopenharmony_ci     - v12.16.0
3981cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/27867
3991cb0ef41Sopenharmony_ci    description: Added captureRejections option.
4001cb0ef41Sopenharmony_ci-->
4011cb0ef41Sopenharmony_ci
4021cb0ef41Sopenharmony_ciThe `EventEmitter` class is defined and exposed by the `node:events` module:
4031cb0ef41Sopenharmony_ci
4041cb0ef41Sopenharmony_ci```mjs
4051cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
4061cb0ef41Sopenharmony_ci```
4071cb0ef41Sopenharmony_ci
4081cb0ef41Sopenharmony_ci```cjs
4091cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
4101cb0ef41Sopenharmony_ci```
4111cb0ef41Sopenharmony_ci
4121cb0ef41Sopenharmony_ciAll `EventEmitter`s emit the event `'newListener'` when new listeners are
4131cb0ef41Sopenharmony_ciadded and `'removeListener'` when existing listeners are removed.
4141cb0ef41Sopenharmony_ci
4151cb0ef41Sopenharmony_ciIt supports the following option:
4161cb0ef41Sopenharmony_ci
4171cb0ef41Sopenharmony_ci* `captureRejections` {boolean} It enables
4181cb0ef41Sopenharmony_ci  [automatic capturing of promise rejection][capturerejections].
4191cb0ef41Sopenharmony_ci  **Default:** `false`.
4201cb0ef41Sopenharmony_ci
4211cb0ef41Sopenharmony_ci### Event: `'newListener'`
4221cb0ef41Sopenharmony_ci
4231cb0ef41Sopenharmony_ci<!-- YAML
4241cb0ef41Sopenharmony_ciadded: v0.1.26
4251cb0ef41Sopenharmony_ci-->
4261cb0ef41Sopenharmony_ci
4271cb0ef41Sopenharmony_ci* `eventName` {string|symbol} The name of the event being listened for
4281cb0ef41Sopenharmony_ci* `listener` {Function} The event handler function
4291cb0ef41Sopenharmony_ci
4301cb0ef41Sopenharmony_ciThe `EventEmitter` instance will emit its own `'newListener'` event _before_
4311cb0ef41Sopenharmony_cia listener is added to its internal array of listeners.
4321cb0ef41Sopenharmony_ci
4331cb0ef41Sopenharmony_ciListeners registered for the `'newListener'` event are passed the event
4341cb0ef41Sopenharmony_ciname and a reference to the listener being added.
4351cb0ef41Sopenharmony_ci
4361cb0ef41Sopenharmony_ciThe fact that the event is triggered before adding the listener has a subtle
4371cb0ef41Sopenharmony_cibut important side effect: any _additional_ listeners registered to the same
4381cb0ef41Sopenharmony_ci`name` _within_ the `'newListener'` callback are inserted _before_ the
4391cb0ef41Sopenharmony_cilistener that is in the process of being added.
4401cb0ef41Sopenharmony_ci
4411cb0ef41Sopenharmony_ci```mjs
4421cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
4431cb0ef41Sopenharmony_ciclass MyEmitter extends EventEmitter {}
4441cb0ef41Sopenharmony_ci
4451cb0ef41Sopenharmony_ciconst myEmitter = new MyEmitter();
4461cb0ef41Sopenharmony_ci// Only do this once so we don't loop forever
4471cb0ef41Sopenharmony_cimyEmitter.once('newListener', (event, listener) => {
4481cb0ef41Sopenharmony_ci  if (event === 'event') {
4491cb0ef41Sopenharmony_ci    // Insert a new listener in front
4501cb0ef41Sopenharmony_ci    myEmitter.on('event', () => {
4511cb0ef41Sopenharmony_ci      console.log('B');
4521cb0ef41Sopenharmony_ci    });
4531cb0ef41Sopenharmony_ci  }
4541cb0ef41Sopenharmony_ci});
4551cb0ef41Sopenharmony_cimyEmitter.on('event', () => {
4561cb0ef41Sopenharmony_ci  console.log('A');
4571cb0ef41Sopenharmony_ci});
4581cb0ef41Sopenharmony_cimyEmitter.emit('event');
4591cb0ef41Sopenharmony_ci// Prints:
4601cb0ef41Sopenharmony_ci//   B
4611cb0ef41Sopenharmony_ci//   A
4621cb0ef41Sopenharmony_ci```
4631cb0ef41Sopenharmony_ci
4641cb0ef41Sopenharmony_ci```cjs
4651cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
4661cb0ef41Sopenharmony_ciclass MyEmitter extends EventEmitter {}
4671cb0ef41Sopenharmony_ci
4681cb0ef41Sopenharmony_ciconst myEmitter = new MyEmitter();
4691cb0ef41Sopenharmony_ci// Only do this once so we don't loop forever
4701cb0ef41Sopenharmony_cimyEmitter.once('newListener', (event, listener) => {
4711cb0ef41Sopenharmony_ci  if (event === 'event') {
4721cb0ef41Sopenharmony_ci    // Insert a new listener in front
4731cb0ef41Sopenharmony_ci    myEmitter.on('event', () => {
4741cb0ef41Sopenharmony_ci      console.log('B');
4751cb0ef41Sopenharmony_ci    });
4761cb0ef41Sopenharmony_ci  }
4771cb0ef41Sopenharmony_ci});
4781cb0ef41Sopenharmony_cimyEmitter.on('event', () => {
4791cb0ef41Sopenharmony_ci  console.log('A');
4801cb0ef41Sopenharmony_ci});
4811cb0ef41Sopenharmony_cimyEmitter.emit('event');
4821cb0ef41Sopenharmony_ci// Prints:
4831cb0ef41Sopenharmony_ci//   B
4841cb0ef41Sopenharmony_ci//   A
4851cb0ef41Sopenharmony_ci```
4861cb0ef41Sopenharmony_ci
4871cb0ef41Sopenharmony_ci### Event: `'removeListener'`
4881cb0ef41Sopenharmony_ci
4891cb0ef41Sopenharmony_ci<!-- YAML
4901cb0ef41Sopenharmony_ciadded: v0.9.3
4911cb0ef41Sopenharmony_cichanges:
4921cb0ef41Sopenharmony_ci  - version:
4931cb0ef41Sopenharmony_ci    - v6.1.0
4941cb0ef41Sopenharmony_ci    - v4.7.0
4951cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/6394
4961cb0ef41Sopenharmony_ci    description: For listeners attached using `.once()`, the `listener` argument
4971cb0ef41Sopenharmony_ci                 now yields the original listener function.
4981cb0ef41Sopenharmony_ci-->
4991cb0ef41Sopenharmony_ci
5001cb0ef41Sopenharmony_ci* `eventName` {string|symbol} The event name
5011cb0ef41Sopenharmony_ci* `listener` {Function} The event handler function
5021cb0ef41Sopenharmony_ci
5031cb0ef41Sopenharmony_ciThe `'removeListener'` event is emitted _after_ the `listener` is removed.
5041cb0ef41Sopenharmony_ci
5051cb0ef41Sopenharmony_ci### `emitter.addListener(eventName, listener)`
5061cb0ef41Sopenharmony_ci
5071cb0ef41Sopenharmony_ci<!-- YAML
5081cb0ef41Sopenharmony_ciadded: v0.1.26
5091cb0ef41Sopenharmony_ci-->
5101cb0ef41Sopenharmony_ci
5111cb0ef41Sopenharmony_ci* `eventName` {string|symbol}
5121cb0ef41Sopenharmony_ci* `listener` {Function}
5131cb0ef41Sopenharmony_ci
5141cb0ef41Sopenharmony_ciAlias for `emitter.on(eventName, listener)`.
5151cb0ef41Sopenharmony_ci
5161cb0ef41Sopenharmony_ci### `emitter.emit(eventName[, ...args])`
5171cb0ef41Sopenharmony_ci
5181cb0ef41Sopenharmony_ci<!-- YAML
5191cb0ef41Sopenharmony_ciadded: v0.1.26
5201cb0ef41Sopenharmony_ci-->
5211cb0ef41Sopenharmony_ci
5221cb0ef41Sopenharmony_ci* `eventName` {string|symbol}
5231cb0ef41Sopenharmony_ci* `...args` {any}
5241cb0ef41Sopenharmony_ci* Returns: {boolean}
5251cb0ef41Sopenharmony_ci
5261cb0ef41Sopenharmony_ciSynchronously calls each of the listeners registered for the event named
5271cb0ef41Sopenharmony_ci`eventName`, in the order they were registered, passing the supplied arguments
5281cb0ef41Sopenharmony_cito each.
5291cb0ef41Sopenharmony_ci
5301cb0ef41Sopenharmony_ciReturns `true` if the event had listeners, `false` otherwise.
5311cb0ef41Sopenharmony_ci
5321cb0ef41Sopenharmony_ci```mjs
5331cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
5341cb0ef41Sopenharmony_ciconst myEmitter = new EventEmitter();
5351cb0ef41Sopenharmony_ci
5361cb0ef41Sopenharmony_ci// First listener
5371cb0ef41Sopenharmony_cimyEmitter.on('event', function firstListener() {
5381cb0ef41Sopenharmony_ci  console.log('Helloooo! first listener');
5391cb0ef41Sopenharmony_ci});
5401cb0ef41Sopenharmony_ci// Second listener
5411cb0ef41Sopenharmony_cimyEmitter.on('event', function secondListener(arg1, arg2) {
5421cb0ef41Sopenharmony_ci  console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
5431cb0ef41Sopenharmony_ci});
5441cb0ef41Sopenharmony_ci// Third listener
5451cb0ef41Sopenharmony_cimyEmitter.on('event', function thirdListener(...args) {
5461cb0ef41Sopenharmony_ci  const parameters = args.join(', ');
5471cb0ef41Sopenharmony_ci  console.log(`event with parameters ${parameters} in third listener`);
5481cb0ef41Sopenharmony_ci});
5491cb0ef41Sopenharmony_ci
5501cb0ef41Sopenharmony_ciconsole.log(myEmitter.listeners('event'));
5511cb0ef41Sopenharmony_ci
5521cb0ef41Sopenharmony_cimyEmitter.emit('event', 1, 2, 3, 4, 5);
5531cb0ef41Sopenharmony_ci
5541cb0ef41Sopenharmony_ci// Prints:
5551cb0ef41Sopenharmony_ci// [
5561cb0ef41Sopenharmony_ci//   [Function: firstListener],
5571cb0ef41Sopenharmony_ci//   [Function: secondListener],
5581cb0ef41Sopenharmony_ci//   [Function: thirdListener]
5591cb0ef41Sopenharmony_ci// ]
5601cb0ef41Sopenharmony_ci// Helloooo! first listener
5611cb0ef41Sopenharmony_ci// event with parameters 1, 2 in second listener
5621cb0ef41Sopenharmony_ci// event with parameters 1, 2, 3, 4, 5 in third listener
5631cb0ef41Sopenharmony_ci```
5641cb0ef41Sopenharmony_ci
5651cb0ef41Sopenharmony_ci```cjs
5661cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
5671cb0ef41Sopenharmony_ciconst myEmitter = new EventEmitter();
5681cb0ef41Sopenharmony_ci
5691cb0ef41Sopenharmony_ci// First listener
5701cb0ef41Sopenharmony_cimyEmitter.on('event', function firstListener() {
5711cb0ef41Sopenharmony_ci  console.log('Helloooo! first listener');
5721cb0ef41Sopenharmony_ci});
5731cb0ef41Sopenharmony_ci// Second listener
5741cb0ef41Sopenharmony_cimyEmitter.on('event', function secondListener(arg1, arg2) {
5751cb0ef41Sopenharmony_ci  console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
5761cb0ef41Sopenharmony_ci});
5771cb0ef41Sopenharmony_ci// Third listener
5781cb0ef41Sopenharmony_cimyEmitter.on('event', function thirdListener(...args) {
5791cb0ef41Sopenharmony_ci  const parameters = args.join(', ');
5801cb0ef41Sopenharmony_ci  console.log(`event with parameters ${parameters} in third listener`);
5811cb0ef41Sopenharmony_ci});
5821cb0ef41Sopenharmony_ci
5831cb0ef41Sopenharmony_ciconsole.log(myEmitter.listeners('event'));
5841cb0ef41Sopenharmony_ci
5851cb0ef41Sopenharmony_cimyEmitter.emit('event', 1, 2, 3, 4, 5);
5861cb0ef41Sopenharmony_ci
5871cb0ef41Sopenharmony_ci// Prints:
5881cb0ef41Sopenharmony_ci// [
5891cb0ef41Sopenharmony_ci//   [Function: firstListener],
5901cb0ef41Sopenharmony_ci//   [Function: secondListener],
5911cb0ef41Sopenharmony_ci//   [Function: thirdListener]
5921cb0ef41Sopenharmony_ci// ]
5931cb0ef41Sopenharmony_ci// Helloooo! first listener
5941cb0ef41Sopenharmony_ci// event with parameters 1, 2 in second listener
5951cb0ef41Sopenharmony_ci// event with parameters 1, 2, 3, 4, 5 in third listener
5961cb0ef41Sopenharmony_ci```
5971cb0ef41Sopenharmony_ci
5981cb0ef41Sopenharmony_ci### `emitter.eventNames()`
5991cb0ef41Sopenharmony_ci
6001cb0ef41Sopenharmony_ci<!-- YAML
6011cb0ef41Sopenharmony_ciadded: v6.0.0
6021cb0ef41Sopenharmony_ci-->
6031cb0ef41Sopenharmony_ci
6041cb0ef41Sopenharmony_ci* Returns: {Array}
6051cb0ef41Sopenharmony_ci
6061cb0ef41Sopenharmony_ciReturns an array listing the events for which the emitter has registered
6071cb0ef41Sopenharmony_cilisteners. The values in the array are strings or `Symbol`s.
6081cb0ef41Sopenharmony_ci
6091cb0ef41Sopenharmony_ci```mjs
6101cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
6111cb0ef41Sopenharmony_ci
6121cb0ef41Sopenharmony_ciconst myEE = new EventEmitter();
6131cb0ef41Sopenharmony_cimyEE.on('foo', () => {});
6141cb0ef41Sopenharmony_cimyEE.on('bar', () => {});
6151cb0ef41Sopenharmony_ci
6161cb0ef41Sopenharmony_ciconst sym = Symbol('symbol');
6171cb0ef41Sopenharmony_cimyEE.on(sym, () => {});
6181cb0ef41Sopenharmony_ci
6191cb0ef41Sopenharmony_ciconsole.log(myEE.eventNames());
6201cb0ef41Sopenharmony_ci// Prints: [ 'foo', 'bar', Symbol(symbol) ]
6211cb0ef41Sopenharmony_ci```
6221cb0ef41Sopenharmony_ci
6231cb0ef41Sopenharmony_ci```cjs
6241cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
6251cb0ef41Sopenharmony_ci
6261cb0ef41Sopenharmony_ciconst myEE = new EventEmitter();
6271cb0ef41Sopenharmony_cimyEE.on('foo', () => {});
6281cb0ef41Sopenharmony_cimyEE.on('bar', () => {});
6291cb0ef41Sopenharmony_ci
6301cb0ef41Sopenharmony_ciconst sym = Symbol('symbol');
6311cb0ef41Sopenharmony_cimyEE.on(sym, () => {});
6321cb0ef41Sopenharmony_ci
6331cb0ef41Sopenharmony_ciconsole.log(myEE.eventNames());
6341cb0ef41Sopenharmony_ci// Prints: [ 'foo', 'bar', Symbol(symbol) ]
6351cb0ef41Sopenharmony_ci```
6361cb0ef41Sopenharmony_ci
6371cb0ef41Sopenharmony_ci### `emitter.getMaxListeners()`
6381cb0ef41Sopenharmony_ci
6391cb0ef41Sopenharmony_ci<!-- YAML
6401cb0ef41Sopenharmony_ciadded: v1.0.0
6411cb0ef41Sopenharmony_ci-->
6421cb0ef41Sopenharmony_ci
6431cb0ef41Sopenharmony_ci* Returns: {integer}
6441cb0ef41Sopenharmony_ci
6451cb0ef41Sopenharmony_ciReturns the current max listener value for the `EventEmitter` which is either
6461cb0ef41Sopenharmony_ciset by [`emitter.setMaxListeners(n)`][] or defaults to
6471cb0ef41Sopenharmony_ci[`events.defaultMaxListeners`][].
6481cb0ef41Sopenharmony_ci
6491cb0ef41Sopenharmony_ci### `emitter.listenerCount(eventName[, listener])`
6501cb0ef41Sopenharmony_ci
6511cb0ef41Sopenharmony_ci<!-- YAML
6521cb0ef41Sopenharmony_ciadded: v3.2.0
6531cb0ef41Sopenharmony_cichanges:
6541cb0ef41Sopenharmony_ci  - version: v18.16.0
6551cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/46523
6561cb0ef41Sopenharmony_ci    description: Added the `listener` argument.
6571cb0ef41Sopenharmony_ci-->
6581cb0ef41Sopenharmony_ci
6591cb0ef41Sopenharmony_ci* `eventName` {string|symbol} The name of the event being listened for
6601cb0ef41Sopenharmony_ci* `listener` {Function} The event handler function
6611cb0ef41Sopenharmony_ci* Returns: {integer}
6621cb0ef41Sopenharmony_ci
6631cb0ef41Sopenharmony_ciReturns the number of listeners listening for the event named `eventName`.
6641cb0ef41Sopenharmony_ciIf `listener` is provided, it will return how many times the listener is found
6651cb0ef41Sopenharmony_ciin the list of the listeners of the event.
6661cb0ef41Sopenharmony_ci
6671cb0ef41Sopenharmony_ci### `emitter.listeners(eventName)`
6681cb0ef41Sopenharmony_ci
6691cb0ef41Sopenharmony_ci<!-- YAML
6701cb0ef41Sopenharmony_ciadded: v0.1.26
6711cb0ef41Sopenharmony_cichanges:
6721cb0ef41Sopenharmony_ci  - version: v7.0.0
6731cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/6881
6741cb0ef41Sopenharmony_ci    description: For listeners attached using `.once()` this returns the
6751cb0ef41Sopenharmony_ci                 original listeners instead of wrapper functions now.
6761cb0ef41Sopenharmony_ci-->
6771cb0ef41Sopenharmony_ci
6781cb0ef41Sopenharmony_ci* `eventName` {string|symbol}
6791cb0ef41Sopenharmony_ci* Returns: {Function\[]}
6801cb0ef41Sopenharmony_ci
6811cb0ef41Sopenharmony_ciReturns a copy of the array of listeners for the event named `eventName`.
6821cb0ef41Sopenharmony_ci
6831cb0ef41Sopenharmony_ci```js
6841cb0ef41Sopenharmony_ciserver.on('connection', (stream) => {
6851cb0ef41Sopenharmony_ci  console.log('someone connected!');
6861cb0ef41Sopenharmony_ci});
6871cb0ef41Sopenharmony_ciconsole.log(util.inspect(server.listeners('connection')));
6881cb0ef41Sopenharmony_ci// Prints: [ [Function] ]
6891cb0ef41Sopenharmony_ci```
6901cb0ef41Sopenharmony_ci
6911cb0ef41Sopenharmony_ci### `emitter.off(eventName, listener)`
6921cb0ef41Sopenharmony_ci
6931cb0ef41Sopenharmony_ci<!-- YAML
6941cb0ef41Sopenharmony_ciadded: v10.0.0
6951cb0ef41Sopenharmony_ci-->
6961cb0ef41Sopenharmony_ci
6971cb0ef41Sopenharmony_ci* `eventName` {string|symbol}
6981cb0ef41Sopenharmony_ci* `listener` {Function}
6991cb0ef41Sopenharmony_ci* Returns: {EventEmitter}
7001cb0ef41Sopenharmony_ci
7011cb0ef41Sopenharmony_ciAlias for [`emitter.removeListener()`][].
7021cb0ef41Sopenharmony_ci
7031cb0ef41Sopenharmony_ci### `emitter.on(eventName, listener)`
7041cb0ef41Sopenharmony_ci
7051cb0ef41Sopenharmony_ci<!-- YAML
7061cb0ef41Sopenharmony_ciadded: v0.1.101
7071cb0ef41Sopenharmony_ci-->
7081cb0ef41Sopenharmony_ci
7091cb0ef41Sopenharmony_ci* `eventName` {string|symbol} The name of the event.
7101cb0ef41Sopenharmony_ci* `listener` {Function} The callback function
7111cb0ef41Sopenharmony_ci* Returns: {EventEmitter}
7121cb0ef41Sopenharmony_ci
7131cb0ef41Sopenharmony_ciAdds the `listener` function to the end of the listeners array for the
7141cb0ef41Sopenharmony_cievent named `eventName`. No checks are made to see if the `listener` has
7151cb0ef41Sopenharmony_cialready been added. Multiple calls passing the same combination of `eventName`
7161cb0ef41Sopenharmony_ciand `listener` will result in the `listener` being added, and called, multiple
7171cb0ef41Sopenharmony_citimes.
7181cb0ef41Sopenharmony_ci
7191cb0ef41Sopenharmony_ci```js
7201cb0ef41Sopenharmony_ciserver.on('connection', (stream) => {
7211cb0ef41Sopenharmony_ci  console.log('someone connected!');
7221cb0ef41Sopenharmony_ci});
7231cb0ef41Sopenharmony_ci```
7241cb0ef41Sopenharmony_ci
7251cb0ef41Sopenharmony_ciReturns a reference to the `EventEmitter`, so that calls can be chained.
7261cb0ef41Sopenharmony_ci
7271cb0ef41Sopenharmony_ciBy default, event listeners are invoked in the order they are added. The
7281cb0ef41Sopenharmony_ci`emitter.prependListener()` method can be used as an alternative to add the
7291cb0ef41Sopenharmony_cievent listener to the beginning of the listeners array.
7301cb0ef41Sopenharmony_ci
7311cb0ef41Sopenharmony_ci```mjs
7321cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
7331cb0ef41Sopenharmony_ciconst myEE = new EventEmitter();
7341cb0ef41Sopenharmony_cimyEE.on('foo', () => console.log('a'));
7351cb0ef41Sopenharmony_cimyEE.prependListener('foo', () => console.log('b'));
7361cb0ef41Sopenharmony_cimyEE.emit('foo');
7371cb0ef41Sopenharmony_ci// Prints:
7381cb0ef41Sopenharmony_ci//   b
7391cb0ef41Sopenharmony_ci//   a
7401cb0ef41Sopenharmony_ci```
7411cb0ef41Sopenharmony_ci
7421cb0ef41Sopenharmony_ci```cjs
7431cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
7441cb0ef41Sopenharmony_ciconst myEE = new EventEmitter();
7451cb0ef41Sopenharmony_cimyEE.on('foo', () => console.log('a'));
7461cb0ef41Sopenharmony_cimyEE.prependListener('foo', () => console.log('b'));
7471cb0ef41Sopenharmony_cimyEE.emit('foo');
7481cb0ef41Sopenharmony_ci// Prints:
7491cb0ef41Sopenharmony_ci//   b
7501cb0ef41Sopenharmony_ci//   a
7511cb0ef41Sopenharmony_ci```
7521cb0ef41Sopenharmony_ci
7531cb0ef41Sopenharmony_ci### `emitter.once(eventName, listener)`
7541cb0ef41Sopenharmony_ci
7551cb0ef41Sopenharmony_ci<!-- YAML
7561cb0ef41Sopenharmony_ciadded: v0.3.0
7571cb0ef41Sopenharmony_ci-->
7581cb0ef41Sopenharmony_ci
7591cb0ef41Sopenharmony_ci* `eventName` {string|symbol} The name of the event.
7601cb0ef41Sopenharmony_ci* `listener` {Function} The callback function
7611cb0ef41Sopenharmony_ci* Returns: {EventEmitter}
7621cb0ef41Sopenharmony_ci
7631cb0ef41Sopenharmony_ciAdds a **one-time** `listener` function for the event named `eventName`. The
7641cb0ef41Sopenharmony_cinext time `eventName` is triggered, this listener is removed and then invoked.
7651cb0ef41Sopenharmony_ci
7661cb0ef41Sopenharmony_ci```js
7671cb0ef41Sopenharmony_ciserver.once('connection', (stream) => {
7681cb0ef41Sopenharmony_ci  console.log('Ah, we have our first user!');
7691cb0ef41Sopenharmony_ci});
7701cb0ef41Sopenharmony_ci```
7711cb0ef41Sopenharmony_ci
7721cb0ef41Sopenharmony_ciReturns a reference to the `EventEmitter`, so that calls can be chained.
7731cb0ef41Sopenharmony_ci
7741cb0ef41Sopenharmony_ciBy default, event listeners are invoked in the order they are added. The
7751cb0ef41Sopenharmony_ci`emitter.prependOnceListener()` method can be used as an alternative to add the
7761cb0ef41Sopenharmony_cievent listener to the beginning of the listeners array.
7771cb0ef41Sopenharmony_ci
7781cb0ef41Sopenharmony_ci```mjs
7791cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
7801cb0ef41Sopenharmony_ciconst myEE = new EventEmitter();
7811cb0ef41Sopenharmony_cimyEE.once('foo', () => console.log('a'));
7821cb0ef41Sopenharmony_cimyEE.prependOnceListener('foo', () => console.log('b'));
7831cb0ef41Sopenharmony_cimyEE.emit('foo');
7841cb0ef41Sopenharmony_ci// Prints:
7851cb0ef41Sopenharmony_ci//   b
7861cb0ef41Sopenharmony_ci//   a
7871cb0ef41Sopenharmony_ci```
7881cb0ef41Sopenharmony_ci
7891cb0ef41Sopenharmony_ci```cjs
7901cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
7911cb0ef41Sopenharmony_ciconst myEE = new EventEmitter();
7921cb0ef41Sopenharmony_cimyEE.once('foo', () => console.log('a'));
7931cb0ef41Sopenharmony_cimyEE.prependOnceListener('foo', () => console.log('b'));
7941cb0ef41Sopenharmony_cimyEE.emit('foo');
7951cb0ef41Sopenharmony_ci// Prints:
7961cb0ef41Sopenharmony_ci//   b
7971cb0ef41Sopenharmony_ci//   a
7981cb0ef41Sopenharmony_ci```
7991cb0ef41Sopenharmony_ci
8001cb0ef41Sopenharmony_ci### `emitter.prependListener(eventName, listener)`
8011cb0ef41Sopenharmony_ci
8021cb0ef41Sopenharmony_ci<!-- YAML
8031cb0ef41Sopenharmony_ciadded: v6.0.0
8041cb0ef41Sopenharmony_ci-->
8051cb0ef41Sopenharmony_ci
8061cb0ef41Sopenharmony_ci* `eventName` {string|symbol} The name of the event.
8071cb0ef41Sopenharmony_ci* `listener` {Function} The callback function
8081cb0ef41Sopenharmony_ci* Returns: {EventEmitter}
8091cb0ef41Sopenharmony_ci
8101cb0ef41Sopenharmony_ciAdds the `listener` function to the _beginning_ of the listeners array for the
8111cb0ef41Sopenharmony_cievent named `eventName`. No checks are made to see if the `listener` has
8121cb0ef41Sopenharmony_cialready been added. Multiple calls passing the same combination of `eventName`
8131cb0ef41Sopenharmony_ciand `listener` will result in the `listener` being added, and called, multiple
8141cb0ef41Sopenharmony_citimes.
8151cb0ef41Sopenharmony_ci
8161cb0ef41Sopenharmony_ci```js
8171cb0ef41Sopenharmony_ciserver.prependListener('connection', (stream) => {
8181cb0ef41Sopenharmony_ci  console.log('someone connected!');
8191cb0ef41Sopenharmony_ci});
8201cb0ef41Sopenharmony_ci```
8211cb0ef41Sopenharmony_ci
8221cb0ef41Sopenharmony_ciReturns a reference to the `EventEmitter`, so that calls can be chained.
8231cb0ef41Sopenharmony_ci
8241cb0ef41Sopenharmony_ci### `emitter.prependOnceListener(eventName, listener)`
8251cb0ef41Sopenharmony_ci
8261cb0ef41Sopenharmony_ci<!-- YAML
8271cb0ef41Sopenharmony_ciadded: v6.0.0
8281cb0ef41Sopenharmony_ci-->
8291cb0ef41Sopenharmony_ci
8301cb0ef41Sopenharmony_ci* `eventName` {string|symbol} The name of the event.
8311cb0ef41Sopenharmony_ci* `listener` {Function} The callback function
8321cb0ef41Sopenharmony_ci* Returns: {EventEmitter}
8331cb0ef41Sopenharmony_ci
8341cb0ef41Sopenharmony_ciAdds a **one-time** `listener` function for the event named `eventName` to the
8351cb0ef41Sopenharmony_ci_beginning_ of the listeners array. The next time `eventName` is triggered, this
8361cb0ef41Sopenharmony_cilistener is removed, and then invoked.
8371cb0ef41Sopenharmony_ci
8381cb0ef41Sopenharmony_ci```js
8391cb0ef41Sopenharmony_ciserver.prependOnceListener('connection', (stream) => {
8401cb0ef41Sopenharmony_ci  console.log('Ah, we have our first user!');
8411cb0ef41Sopenharmony_ci});
8421cb0ef41Sopenharmony_ci```
8431cb0ef41Sopenharmony_ci
8441cb0ef41Sopenharmony_ciReturns a reference to the `EventEmitter`, so that calls can be chained.
8451cb0ef41Sopenharmony_ci
8461cb0ef41Sopenharmony_ci### `emitter.removeAllListeners([eventName])`
8471cb0ef41Sopenharmony_ci
8481cb0ef41Sopenharmony_ci<!-- YAML
8491cb0ef41Sopenharmony_ciadded: v0.1.26
8501cb0ef41Sopenharmony_ci-->
8511cb0ef41Sopenharmony_ci
8521cb0ef41Sopenharmony_ci* `eventName` {string|symbol}
8531cb0ef41Sopenharmony_ci* Returns: {EventEmitter}
8541cb0ef41Sopenharmony_ci
8551cb0ef41Sopenharmony_ciRemoves all listeners, or those of the specified `eventName`.
8561cb0ef41Sopenharmony_ci
8571cb0ef41Sopenharmony_ciIt is bad practice to remove listeners added elsewhere in the code,
8581cb0ef41Sopenharmony_ciparticularly when the `EventEmitter` instance was created by some other
8591cb0ef41Sopenharmony_cicomponent or module (e.g. sockets or file streams).
8601cb0ef41Sopenharmony_ci
8611cb0ef41Sopenharmony_ciReturns a reference to the `EventEmitter`, so that calls can be chained.
8621cb0ef41Sopenharmony_ci
8631cb0ef41Sopenharmony_ci### `emitter.removeListener(eventName, listener)`
8641cb0ef41Sopenharmony_ci
8651cb0ef41Sopenharmony_ci<!-- YAML
8661cb0ef41Sopenharmony_ciadded: v0.1.26
8671cb0ef41Sopenharmony_ci-->
8681cb0ef41Sopenharmony_ci
8691cb0ef41Sopenharmony_ci* `eventName` {string|symbol}
8701cb0ef41Sopenharmony_ci* `listener` {Function}
8711cb0ef41Sopenharmony_ci* Returns: {EventEmitter}
8721cb0ef41Sopenharmony_ci
8731cb0ef41Sopenharmony_ciRemoves the specified `listener` from the listener array for the event named
8741cb0ef41Sopenharmony_ci`eventName`.
8751cb0ef41Sopenharmony_ci
8761cb0ef41Sopenharmony_ci```js
8771cb0ef41Sopenharmony_ciconst callback = (stream) => {
8781cb0ef41Sopenharmony_ci  console.log('someone connected!');
8791cb0ef41Sopenharmony_ci};
8801cb0ef41Sopenharmony_ciserver.on('connection', callback);
8811cb0ef41Sopenharmony_ci// ...
8821cb0ef41Sopenharmony_ciserver.removeListener('connection', callback);
8831cb0ef41Sopenharmony_ci```
8841cb0ef41Sopenharmony_ci
8851cb0ef41Sopenharmony_ci`removeListener()` will remove, at most, one instance of a listener from the
8861cb0ef41Sopenharmony_cilistener array. If any single listener has been added multiple times to the
8871cb0ef41Sopenharmony_cilistener array for the specified `eventName`, then `removeListener()` must be
8881cb0ef41Sopenharmony_cicalled multiple times to remove each instance.
8891cb0ef41Sopenharmony_ci
8901cb0ef41Sopenharmony_ciOnce an event is emitted, all listeners attached to it at the
8911cb0ef41Sopenharmony_citime of emitting are called in order. This implies that any
8921cb0ef41Sopenharmony_ci`removeListener()` or `removeAllListeners()` calls _after_ emitting and
8931cb0ef41Sopenharmony_ci_before_ the last listener finishes execution will not remove them from
8941cb0ef41Sopenharmony_ci`emit()` in progress. Subsequent events behave as expected.
8951cb0ef41Sopenharmony_ci
8961cb0ef41Sopenharmony_ci```mjs
8971cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
8981cb0ef41Sopenharmony_ciclass MyEmitter extends EventEmitter {}
8991cb0ef41Sopenharmony_ciconst myEmitter = new MyEmitter();
9001cb0ef41Sopenharmony_ci
9011cb0ef41Sopenharmony_ciconst callbackA = () => {
9021cb0ef41Sopenharmony_ci  console.log('A');
9031cb0ef41Sopenharmony_ci  myEmitter.removeListener('event', callbackB);
9041cb0ef41Sopenharmony_ci};
9051cb0ef41Sopenharmony_ci
9061cb0ef41Sopenharmony_ciconst callbackB = () => {
9071cb0ef41Sopenharmony_ci  console.log('B');
9081cb0ef41Sopenharmony_ci};
9091cb0ef41Sopenharmony_ci
9101cb0ef41Sopenharmony_cimyEmitter.on('event', callbackA);
9111cb0ef41Sopenharmony_ci
9121cb0ef41Sopenharmony_cimyEmitter.on('event', callbackB);
9131cb0ef41Sopenharmony_ci
9141cb0ef41Sopenharmony_ci// callbackA removes listener callbackB but it will still be called.
9151cb0ef41Sopenharmony_ci// Internal listener array at time of emit [callbackA, callbackB]
9161cb0ef41Sopenharmony_cimyEmitter.emit('event');
9171cb0ef41Sopenharmony_ci// Prints:
9181cb0ef41Sopenharmony_ci//   A
9191cb0ef41Sopenharmony_ci//   B
9201cb0ef41Sopenharmony_ci
9211cb0ef41Sopenharmony_ci// callbackB is now removed.
9221cb0ef41Sopenharmony_ci// Internal listener array [callbackA]
9231cb0ef41Sopenharmony_cimyEmitter.emit('event');
9241cb0ef41Sopenharmony_ci// Prints:
9251cb0ef41Sopenharmony_ci//   A
9261cb0ef41Sopenharmony_ci```
9271cb0ef41Sopenharmony_ci
9281cb0ef41Sopenharmony_ci```cjs
9291cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
9301cb0ef41Sopenharmony_ciclass MyEmitter extends EventEmitter {}
9311cb0ef41Sopenharmony_ciconst myEmitter = new MyEmitter();
9321cb0ef41Sopenharmony_ci
9331cb0ef41Sopenharmony_ciconst callbackA = () => {
9341cb0ef41Sopenharmony_ci  console.log('A');
9351cb0ef41Sopenharmony_ci  myEmitter.removeListener('event', callbackB);
9361cb0ef41Sopenharmony_ci};
9371cb0ef41Sopenharmony_ci
9381cb0ef41Sopenharmony_ciconst callbackB = () => {
9391cb0ef41Sopenharmony_ci  console.log('B');
9401cb0ef41Sopenharmony_ci};
9411cb0ef41Sopenharmony_ci
9421cb0ef41Sopenharmony_cimyEmitter.on('event', callbackA);
9431cb0ef41Sopenharmony_ci
9441cb0ef41Sopenharmony_cimyEmitter.on('event', callbackB);
9451cb0ef41Sopenharmony_ci
9461cb0ef41Sopenharmony_ci// callbackA removes listener callbackB but it will still be called.
9471cb0ef41Sopenharmony_ci// Internal listener array at time of emit [callbackA, callbackB]
9481cb0ef41Sopenharmony_cimyEmitter.emit('event');
9491cb0ef41Sopenharmony_ci// Prints:
9501cb0ef41Sopenharmony_ci//   A
9511cb0ef41Sopenharmony_ci//   B
9521cb0ef41Sopenharmony_ci
9531cb0ef41Sopenharmony_ci// callbackB is now removed.
9541cb0ef41Sopenharmony_ci// Internal listener array [callbackA]
9551cb0ef41Sopenharmony_cimyEmitter.emit('event');
9561cb0ef41Sopenharmony_ci// Prints:
9571cb0ef41Sopenharmony_ci//   A
9581cb0ef41Sopenharmony_ci```
9591cb0ef41Sopenharmony_ci
9601cb0ef41Sopenharmony_ciBecause listeners are managed using an internal array, calling this will
9611cb0ef41Sopenharmony_cichange the position indices of any listener registered _after_ the listener
9621cb0ef41Sopenharmony_cibeing removed. This will not impact the order in which listeners are called,
9631cb0ef41Sopenharmony_cibut it means that any copies of the listener array as returned by
9641cb0ef41Sopenharmony_cithe `emitter.listeners()` method will need to be recreated.
9651cb0ef41Sopenharmony_ci
9661cb0ef41Sopenharmony_ciWhen a single function has been added as a handler multiple times for a single
9671cb0ef41Sopenharmony_cievent (as in the example below), `removeListener()` will remove the most
9681cb0ef41Sopenharmony_cirecently added instance. In the example the `once('ping')`
9691cb0ef41Sopenharmony_cilistener is removed:
9701cb0ef41Sopenharmony_ci
9711cb0ef41Sopenharmony_ci```mjs
9721cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
9731cb0ef41Sopenharmony_ciconst ee = new EventEmitter();
9741cb0ef41Sopenharmony_ci
9751cb0ef41Sopenharmony_cifunction pong() {
9761cb0ef41Sopenharmony_ci  console.log('pong');
9771cb0ef41Sopenharmony_ci}
9781cb0ef41Sopenharmony_ci
9791cb0ef41Sopenharmony_ciee.on('ping', pong);
9801cb0ef41Sopenharmony_ciee.once('ping', pong);
9811cb0ef41Sopenharmony_ciee.removeListener('ping', pong);
9821cb0ef41Sopenharmony_ci
9831cb0ef41Sopenharmony_ciee.emit('ping');
9841cb0ef41Sopenharmony_ciee.emit('ping');
9851cb0ef41Sopenharmony_ci```
9861cb0ef41Sopenharmony_ci
9871cb0ef41Sopenharmony_ci```cjs
9881cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
9891cb0ef41Sopenharmony_ciconst ee = new EventEmitter();
9901cb0ef41Sopenharmony_ci
9911cb0ef41Sopenharmony_cifunction pong() {
9921cb0ef41Sopenharmony_ci  console.log('pong');
9931cb0ef41Sopenharmony_ci}
9941cb0ef41Sopenharmony_ci
9951cb0ef41Sopenharmony_ciee.on('ping', pong);
9961cb0ef41Sopenharmony_ciee.once('ping', pong);
9971cb0ef41Sopenharmony_ciee.removeListener('ping', pong);
9981cb0ef41Sopenharmony_ci
9991cb0ef41Sopenharmony_ciee.emit('ping');
10001cb0ef41Sopenharmony_ciee.emit('ping');
10011cb0ef41Sopenharmony_ci```
10021cb0ef41Sopenharmony_ci
10031cb0ef41Sopenharmony_ciReturns a reference to the `EventEmitter`, so that calls can be chained.
10041cb0ef41Sopenharmony_ci
10051cb0ef41Sopenharmony_ci### `emitter.setMaxListeners(n)`
10061cb0ef41Sopenharmony_ci
10071cb0ef41Sopenharmony_ci<!-- YAML
10081cb0ef41Sopenharmony_ciadded: v0.3.5
10091cb0ef41Sopenharmony_ci-->
10101cb0ef41Sopenharmony_ci
10111cb0ef41Sopenharmony_ci* `n` {integer}
10121cb0ef41Sopenharmony_ci* Returns: {EventEmitter}
10131cb0ef41Sopenharmony_ci
10141cb0ef41Sopenharmony_ciBy default `EventEmitter`s will print a warning if more than `10` listeners are
10151cb0ef41Sopenharmony_ciadded for a particular event. This is a useful default that helps finding
10161cb0ef41Sopenharmony_cimemory leaks. The `emitter.setMaxListeners()` method allows the limit to be
10171cb0ef41Sopenharmony_cimodified for this specific `EventEmitter` instance. The value can be set to
10181cb0ef41Sopenharmony_ci`Infinity` (or `0`) to indicate an unlimited number of listeners.
10191cb0ef41Sopenharmony_ci
10201cb0ef41Sopenharmony_ciReturns a reference to the `EventEmitter`, so that calls can be chained.
10211cb0ef41Sopenharmony_ci
10221cb0ef41Sopenharmony_ci### `emitter.rawListeners(eventName)`
10231cb0ef41Sopenharmony_ci
10241cb0ef41Sopenharmony_ci<!-- YAML
10251cb0ef41Sopenharmony_ciadded: v9.4.0
10261cb0ef41Sopenharmony_ci-->
10271cb0ef41Sopenharmony_ci
10281cb0ef41Sopenharmony_ci* `eventName` {string|symbol}
10291cb0ef41Sopenharmony_ci* Returns: {Function\[]}
10301cb0ef41Sopenharmony_ci
10311cb0ef41Sopenharmony_ciReturns a copy of the array of listeners for the event named `eventName`,
10321cb0ef41Sopenharmony_ciincluding any wrappers (such as those created by `.once()`).
10331cb0ef41Sopenharmony_ci
10341cb0ef41Sopenharmony_ci```mjs
10351cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
10361cb0ef41Sopenharmony_ciconst emitter = new EventEmitter();
10371cb0ef41Sopenharmony_ciemitter.once('log', () => console.log('log once'));
10381cb0ef41Sopenharmony_ci
10391cb0ef41Sopenharmony_ci// Returns a new Array with a function `onceWrapper` which has a property
10401cb0ef41Sopenharmony_ci// `listener` which contains the original listener bound above
10411cb0ef41Sopenharmony_ciconst listeners = emitter.rawListeners('log');
10421cb0ef41Sopenharmony_ciconst logFnWrapper = listeners[0];
10431cb0ef41Sopenharmony_ci
10441cb0ef41Sopenharmony_ci// Logs "log once" to the console and does not unbind the `once` event
10451cb0ef41Sopenharmony_cilogFnWrapper.listener();
10461cb0ef41Sopenharmony_ci
10471cb0ef41Sopenharmony_ci// Logs "log once" to the console and removes the listener
10481cb0ef41Sopenharmony_cilogFnWrapper();
10491cb0ef41Sopenharmony_ci
10501cb0ef41Sopenharmony_ciemitter.on('log', () => console.log('log persistently'));
10511cb0ef41Sopenharmony_ci// Will return a new Array with a single function bound by `.on()` above
10521cb0ef41Sopenharmony_ciconst newListeners = emitter.rawListeners('log');
10531cb0ef41Sopenharmony_ci
10541cb0ef41Sopenharmony_ci// Logs "log persistently" twice
10551cb0ef41Sopenharmony_cinewListeners[0]();
10561cb0ef41Sopenharmony_ciemitter.emit('log');
10571cb0ef41Sopenharmony_ci```
10581cb0ef41Sopenharmony_ci
10591cb0ef41Sopenharmony_ci```cjs
10601cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
10611cb0ef41Sopenharmony_ciconst emitter = new EventEmitter();
10621cb0ef41Sopenharmony_ciemitter.once('log', () => console.log('log once'));
10631cb0ef41Sopenharmony_ci
10641cb0ef41Sopenharmony_ci// Returns a new Array with a function `onceWrapper` which has a property
10651cb0ef41Sopenharmony_ci// `listener` which contains the original listener bound above
10661cb0ef41Sopenharmony_ciconst listeners = emitter.rawListeners('log');
10671cb0ef41Sopenharmony_ciconst logFnWrapper = listeners[0];
10681cb0ef41Sopenharmony_ci
10691cb0ef41Sopenharmony_ci// Logs "log once" to the console and does not unbind the `once` event
10701cb0ef41Sopenharmony_cilogFnWrapper.listener();
10711cb0ef41Sopenharmony_ci
10721cb0ef41Sopenharmony_ci// Logs "log once" to the console and removes the listener
10731cb0ef41Sopenharmony_cilogFnWrapper();
10741cb0ef41Sopenharmony_ci
10751cb0ef41Sopenharmony_ciemitter.on('log', () => console.log('log persistently'));
10761cb0ef41Sopenharmony_ci// Will return a new Array with a single function bound by `.on()` above
10771cb0ef41Sopenharmony_ciconst newListeners = emitter.rawListeners('log');
10781cb0ef41Sopenharmony_ci
10791cb0ef41Sopenharmony_ci// Logs "log persistently" twice
10801cb0ef41Sopenharmony_cinewListeners[0]();
10811cb0ef41Sopenharmony_ciemitter.emit('log');
10821cb0ef41Sopenharmony_ci```
10831cb0ef41Sopenharmony_ci
10841cb0ef41Sopenharmony_ci### `emitter[Symbol.for('nodejs.rejection')](err, eventName[, ...args])`
10851cb0ef41Sopenharmony_ci
10861cb0ef41Sopenharmony_ci<!-- YAML
10871cb0ef41Sopenharmony_ciadded:
10881cb0ef41Sopenharmony_ci - v13.4.0
10891cb0ef41Sopenharmony_ci - v12.16.0
10901cb0ef41Sopenharmony_cichanges:
10911cb0ef41Sopenharmony_ci  - version:
10921cb0ef41Sopenharmony_ci    - v17.4.0
10931cb0ef41Sopenharmony_ci    - v16.14.0
10941cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/41267
10951cb0ef41Sopenharmony_ci    description: No longer experimental.
10961cb0ef41Sopenharmony_ci-->
10971cb0ef41Sopenharmony_ci
10981cb0ef41Sopenharmony_ci* `err` Error
10991cb0ef41Sopenharmony_ci* `eventName` {string|symbol}
11001cb0ef41Sopenharmony_ci* `...args` {any}
11011cb0ef41Sopenharmony_ci
11021cb0ef41Sopenharmony_ciThe `Symbol.for('nodejs.rejection')` method is called in case a
11031cb0ef41Sopenharmony_cipromise rejection happens when emitting an event and
11041cb0ef41Sopenharmony_ci[`captureRejections`][capturerejections] is enabled on the emitter.
11051cb0ef41Sopenharmony_ciIt is possible to use [`events.captureRejectionSymbol`][rejectionsymbol] in
11061cb0ef41Sopenharmony_ciplace of `Symbol.for('nodejs.rejection')`.
11071cb0ef41Sopenharmony_ci
11081cb0ef41Sopenharmony_ci```mjs
11091cb0ef41Sopenharmony_ciimport { EventEmitter, captureRejectionSymbol } from 'node:events';
11101cb0ef41Sopenharmony_ci
11111cb0ef41Sopenharmony_ciclass MyClass extends EventEmitter {
11121cb0ef41Sopenharmony_ci  constructor() {
11131cb0ef41Sopenharmony_ci    super({ captureRejections: true });
11141cb0ef41Sopenharmony_ci  }
11151cb0ef41Sopenharmony_ci
11161cb0ef41Sopenharmony_ci  [captureRejectionSymbol](err, event, ...args) {
11171cb0ef41Sopenharmony_ci    console.log('rejection happened for', event, 'with', err, ...args);
11181cb0ef41Sopenharmony_ci    this.destroy(err);
11191cb0ef41Sopenharmony_ci  }
11201cb0ef41Sopenharmony_ci
11211cb0ef41Sopenharmony_ci  destroy(err) {
11221cb0ef41Sopenharmony_ci    // Tear the resource down here.
11231cb0ef41Sopenharmony_ci  }
11241cb0ef41Sopenharmony_ci}
11251cb0ef41Sopenharmony_ci```
11261cb0ef41Sopenharmony_ci
11271cb0ef41Sopenharmony_ci```cjs
11281cb0ef41Sopenharmony_ciconst { EventEmitter, captureRejectionSymbol } = require('node:events');
11291cb0ef41Sopenharmony_ci
11301cb0ef41Sopenharmony_ciclass MyClass extends EventEmitter {
11311cb0ef41Sopenharmony_ci  constructor() {
11321cb0ef41Sopenharmony_ci    super({ captureRejections: true });
11331cb0ef41Sopenharmony_ci  }
11341cb0ef41Sopenharmony_ci
11351cb0ef41Sopenharmony_ci  [captureRejectionSymbol](err, event, ...args) {
11361cb0ef41Sopenharmony_ci    console.log('rejection happened for', event, 'with', err, ...args);
11371cb0ef41Sopenharmony_ci    this.destroy(err);
11381cb0ef41Sopenharmony_ci  }
11391cb0ef41Sopenharmony_ci
11401cb0ef41Sopenharmony_ci  destroy(err) {
11411cb0ef41Sopenharmony_ci    // Tear the resource down here.
11421cb0ef41Sopenharmony_ci  }
11431cb0ef41Sopenharmony_ci}
11441cb0ef41Sopenharmony_ci```
11451cb0ef41Sopenharmony_ci
11461cb0ef41Sopenharmony_ci## `events.defaultMaxListeners`
11471cb0ef41Sopenharmony_ci
11481cb0ef41Sopenharmony_ci<!-- YAML
11491cb0ef41Sopenharmony_ciadded: v0.11.2
11501cb0ef41Sopenharmony_ci-->
11511cb0ef41Sopenharmony_ci
11521cb0ef41Sopenharmony_ciBy default, a maximum of `10` listeners can be registered for any single
11531cb0ef41Sopenharmony_cievent. This limit can be changed for individual `EventEmitter` instances
11541cb0ef41Sopenharmony_ciusing the [`emitter.setMaxListeners(n)`][] method. To change the default
11551cb0ef41Sopenharmony_cifor _all_ `EventEmitter` instances, the `events.defaultMaxListeners`
11561cb0ef41Sopenharmony_ciproperty can be used. If this value is not a positive number, a `RangeError`
11571cb0ef41Sopenharmony_ciis thrown.
11581cb0ef41Sopenharmony_ci
11591cb0ef41Sopenharmony_ciTake caution when setting the `events.defaultMaxListeners` because the
11601cb0ef41Sopenharmony_cichange affects _all_ `EventEmitter` instances, including those created before
11611cb0ef41Sopenharmony_cithe change is made. However, calling [`emitter.setMaxListeners(n)`][] still has
11621cb0ef41Sopenharmony_ciprecedence over `events.defaultMaxListeners`.
11631cb0ef41Sopenharmony_ci
11641cb0ef41Sopenharmony_ciThis is not a hard limit. The `EventEmitter` instance will allow
11651cb0ef41Sopenharmony_cimore listeners to be added but will output a trace warning to stderr indicating
11661cb0ef41Sopenharmony_cithat a "possible EventEmitter memory leak" has been detected. For any single
11671cb0ef41Sopenharmony_ci`EventEmitter`, the `emitter.getMaxListeners()` and `emitter.setMaxListeners()`
11681cb0ef41Sopenharmony_cimethods can be used to temporarily avoid this warning:
11691cb0ef41Sopenharmony_ci
11701cb0ef41Sopenharmony_ci```mjs
11711cb0ef41Sopenharmony_ciimport { EventEmitter } from 'node:events';
11721cb0ef41Sopenharmony_ciconst emitter = new EventEmitter();
11731cb0ef41Sopenharmony_ciemitter.setMaxListeners(emitter.getMaxListeners() + 1);
11741cb0ef41Sopenharmony_ciemitter.once('event', () => {
11751cb0ef41Sopenharmony_ci  // do stuff
11761cb0ef41Sopenharmony_ci  emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));
11771cb0ef41Sopenharmony_ci});
11781cb0ef41Sopenharmony_ci```
11791cb0ef41Sopenharmony_ci
11801cb0ef41Sopenharmony_ci```cjs
11811cb0ef41Sopenharmony_ciconst EventEmitter = require('node:events');
11821cb0ef41Sopenharmony_ciconst emitter = new EventEmitter();
11831cb0ef41Sopenharmony_ciemitter.setMaxListeners(emitter.getMaxListeners() + 1);
11841cb0ef41Sopenharmony_ciemitter.once('event', () => {
11851cb0ef41Sopenharmony_ci  // do stuff
11861cb0ef41Sopenharmony_ci  emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));
11871cb0ef41Sopenharmony_ci});
11881cb0ef41Sopenharmony_ci```
11891cb0ef41Sopenharmony_ci
11901cb0ef41Sopenharmony_ciThe [`--trace-warnings`][] command-line flag can be used to display the
11911cb0ef41Sopenharmony_cistack trace for such warnings.
11921cb0ef41Sopenharmony_ci
11931cb0ef41Sopenharmony_ciThe emitted warning can be inspected with [`process.on('warning')`][] and will
11941cb0ef41Sopenharmony_cihave the additional `emitter`, `type`, and `count` properties, referring to
11951cb0ef41Sopenharmony_cithe event emitter instance, the event's name and the number of attached
11961cb0ef41Sopenharmony_cilisteners, respectively.
11971cb0ef41Sopenharmony_ciIts `name` property is set to `'MaxListenersExceededWarning'`.
11981cb0ef41Sopenharmony_ci
11991cb0ef41Sopenharmony_ci## `events.errorMonitor`
12001cb0ef41Sopenharmony_ci
12011cb0ef41Sopenharmony_ci<!-- YAML
12021cb0ef41Sopenharmony_ciadded:
12031cb0ef41Sopenharmony_ci - v13.6.0
12041cb0ef41Sopenharmony_ci - v12.17.0
12051cb0ef41Sopenharmony_ci-->
12061cb0ef41Sopenharmony_ci
12071cb0ef41Sopenharmony_ciThis symbol shall be used to install a listener for only monitoring `'error'`
12081cb0ef41Sopenharmony_cievents. Listeners installed using this symbol are called before the regular
12091cb0ef41Sopenharmony_ci`'error'` listeners are called.
12101cb0ef41Sopenharmony_ci
12111cb0ef41Sopenharmony_ciInstalling a listener using this symbol does not change the behavior once an
12121cb0ef41Sopenharmony_ci`'error'` event is emitted. Therefore, the process will still crash if no
12131cb0ef41Sopenharmony_ciregular `'error'` listener is installed.
12141cb0ef41Sopenharmony_ci
12151cb0ef41Sopenharmony_ci## `events.getEventListeners(emitterOrTarget, eventName)`
12161cb0ef41Sopenharmony_ci
12171cb0ef41Sopenharmony_ci<!-- YAML
12181cb0ef41Sopenharmony_ciadded:
12191cb0ef41Sopenharmony_ci - v15.2.0
12201cb0ef41Sopenharmony_ci - v14.17.0
12211cb0ef41Sopenharmony_ci-->
12221cb0ef41Sopenharmony_ci
12231cb0ef41Sopenharmony_ci* `emitterOrTarget` {EventEmitter|EventTarget}
12241cb0ef41Sopenharmony_ci* `eventName` {string|symbol}
12251cb0ef41Sopenharmony_ci* Returns: {Function\[]}
12261cb0ef41Sopenharmony_ci
12271cb0ef41Sopenharmony_ciReturns a copy of the array of listeners for the event named `eventName`.
12281cb0ef41Sopenharmony_ci
12291cb0ef41Sopenharmony_ciFor `EventEmitter`s this behaves exactly the same as calling `.listeners` on
12301cb0ef41Sopenharmony_cithe emitter.
12311cb0ef41Sopenharmony_ci
12321cb0ef41Sopenharmony_ciFor `EventTarget`s this is the only way to get the event listeners for the
12331cb0ef41Sopenharmony_cievent target. This is useful for debugging and diagnostic purposes.
12341cb0ef41Sopenharmony_ci
12351cb0ef41Sopenharmony_ci```mjs
12361cb0ef41Sopenharmony_ciimport { getEventListeners, EventEmitter } from 'node:events';
12371cb0ef41Sopenharmony_ci
12381cb0ef41Sopenharmony_ci{
12391cb0ef41Sopenharmony_ci  const ee = new EventEmitter();
12401cb0ef41Sopenharmony_ci  const listener = () => console.log('Events are fun');
12411cb0ef41Sopenharmony_ci  ee.on('foo', listener);
12421cb0ef41Sopenharmony_ci  console.log(getEventListeners(ee, 'foo')); // [ [Function: listener] ]
12431cb0ef41Sopenharmony_ci}
12441cb0ef41Sopenharmony_ci{
12451cb0ef41Sopenharmony_ci  const et = new EventTarget();
12461cb0ef41Sopenharmony_ci  const listener = () => console.log('Events are fun');
12471cb0ef41Sopenharmony_ci  et.addEventListener('foo', listener);
12481cb0ef41Sopenharmony_ci  console.log(getEventListeners(et, 'foo')); // [ [Function: listener] ]
12491cb0ef41Sopenharmony_ci}
12501cb0ef41Sopenharmony_ci```
12511cb0ef41Sopenharmony_ci
12521cb0ef41Sopenharmony_ci```cjs
12531cb0ef41Sopenharmony_ciconst { getEventListeners, EventEmitter } = require('node:events');
12541cb0ef41Sopenharmony_ci
12551cb0ef41Sopenharmony_ci{
12561cb0ef41Sopenharmony_ci  const ee = new EventEmitter();
12571cb0ef41Sopenharmony_ci  const listener = () => console.log('Events are fun');
12581cb0ef41Sopenharmony_ci  ee.on('foo', listener);
12591cb0ef41Sopenharmony_ci  console.log(getEventListeners(ee, 'foo')); // [ [Function: listener] ]
12601cb0ef41Sopenharmony_ci}
12611cb0ef41Sopenharmony_ci{
12621cb0ef41Sopenharmony_ci  const et = new EventTarget();
12631cb0ef41Sopenharmony_ci  const listener = () => console.log('Events are fun');
12641cb0ef41Sopenharmony_ci  et.addEventListener('foo', listener);
12651cb0ef41Sopenharmony_ci  console.log(getEventListeners(et, 'foo')); // [ [Function: listener] ]
12661cb0ef41Sopenharmony_ci}
12671cb0ef41Sopenharmony_ci```
12681cb0ef41Sopenharmony_ci
12691cb0ef41Sopenharmony_ci## `events.getMaxListeners(emitterOrTarget)`
12701cb0ef41Sopenharmony_ci
12711cb0ef41Sopenharmony_ci<!-- YAML
12721cb0ef41Sopenharmony_ciadded: v18.17.0
12731cb0ef41Sopenharmony_ci-->
12741cb0ef41Sopenharmony_ci
12751cb0ef41Sopenharmony_ci* `emitterOrTarget` {EventEmitter|EventTarget}
12761cb0ef41Sopenharmony_ci* Returns: {number}
12771cb0ef41Sopenharmony_ci
12781cb0ef41Sopenharmony_ciReturns the currently set max amount of listeners.
12791cb0ef41Sopenharmony_ci
12801cb0ef41Sopenharmony_ciFor `EventEmitter`s this behaves exactly the same as calling `.getMaxListeners` on
12811cb0ef41Sopenharmony_cithe emitter.
12821cb0ef41Sopenharmony_ci
12831cb0ef41Sopenharmony_ciFor `EventTarget`s this is the only way to get the max event listeners for the
12841cb0ef41Sopenharmony_cievent target. If the number of event handlers on a single EventTarget exceeds
12851cb0ef41Sopenharmony_cithe max set, the EventTarget will print a warning.
12861cb0ef41Sopenharmony_ci
12871cb0ef41Sopenharmony_ci```mjs
12881cb0ef41Sopenharmony_ciimport { getMaxListeners, setMaxListeners, EventEmitter } from 'node:events';
12891cb0ef41Sopenharmony_ci
12901cb0ef41Sopenharmony_ci{
12911cb0ef41Sopenharmony_ci  const ee = new EventEmitter();
12921cb0ef41Sopenharmony_ci  console.log(getMaxListeners(ee)); // 10
12931cb0ef41Sopenharmony_ci  setMaxListeners(11, ee);
12941cb0ef41Sopenharmony_ci  console.log(getMaxListeners(ee)); // 11
12951cb0ef41Sopenharmony_ci}
12961cb0ef41Sopenharmony_ci{
12971cb0ef41Sopenharmony_ci  const et = new EventTarget();
12981cb0ef41Sopenharmony_ci  console.log(getMaxListeners(et)); // 10
12991cb0ef41Sopenharmony_ci  setMaxListeners(11, et);
13001cb0ef41Sopenharmony_ci  console.log(getMaxListeners(et)); // 11
13011cb0ef41Sopenharmony_ci}
13021cb0ef41Sopenharmony_ci```
13031cb0ef41Sopenharmony_ci
13041cb0ef41Sopenharmony_ci```cjs
13051cb0ef41Sopenharmony_ciconst { getMaxListeners, setMaxListeners, EventEmitter } = require('node:events');
13061cb0ef41Sopenharmony_ci
13071cb0ef41Sopenharmony_ci{
13081cb0ef41Sopenharmony_ci  const ee = new EventEmitter();
13091cb0ef41Sopenharmony_ci  console.log(getMaxListeners(ee)); // 10
13101cb0ef41Sopenharmony_ci  setMaxListeners(11, ee);
13111cb0ef41Sopenharmony_ci  console.log(getMaxListeners(ee)); // 11
13121cb0ef41Sopenharmony_ci}
13131cb0ef41Sopenharmony_ci{
13141cb0ef41Sopenharmony_ci  const et = new EventTarget();
13151cb0ef41Sopenharmony_ci  console.log(getMaxListeners(et)); // 10
13161cb0ef41Sopenharmony_ci  setMaxListeners(11, et);
13171cb0ef41Sopenharmony_ci  console.log(getMaxListeners(et)); // 11
13181cb0ef41Sopenharmony_ci}
13191cb0ef41Sopenharmony_ci```
13201cb0ef41Sopenharmony_ci
13211cb0ef41Sopenharmony_ci## `events.once(emitter, name[, options])`
13221cb0ef41Sopenharmony_ci
13231cb0ef41Sopenharmony_ci<!-- YAML
13241cb0ef41Sopenharmony_ciadded:
13251cb0ef41Sopenharmony_ci - v11.13.0
13261cb0ef41Sopenharmony_ci - v10.16.0
13271cb0ef41Sopenharmony_cichanges:
13281cb0ef41Sopenharmony_ci  - version: v15.0.0
13291cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/34912
13301cb0ef41Sopenharmony_ci    description: The `signal` option is supported now.
13311cb0ef41Sopenharmony_ci-->
13321cb0ef41Sopenharmony_ci
13331cb0ef41Sopenharmony_ci* `emitter` {EventEmitter}
13341cb0ef41Sopenharmony_ci* `name` {string}
13351cb0ef41Sopenharmony_ci* `options` {Object}
13361cb0ef41Sopenharmony_ci  * `signal` {AbortSignal} Can be used to cancel waiting for the event.
13371cb0ef41Sopenharmony_ci* Returns: {Promise}
13381cb0ef41Sopenharmony_ci
13391cb0ef41Sopenharmony_ciCreates a `Promise` that is fulfilled when the `EventEmitter` emits the given
13401cb0ef41Sopenharmony_cievent or that is rejected if the `EventEmitter` emits `'error'` while waiting.
13411cb0ef41Sopenharmony_ciThe `Promise` will resolve with an array of all the arguments emitted to the
13421cb0ef41Sopenharmony_cigiven event.
13431cb0ef41Sopenharmony_ci
13441cb0ef41Sopenharmony_ciThis method is intentionally generic and works with the web platform
13451cb0ef41Sopenharmony_ci[EventTarget][WHATWG-EventTarget] interface, which has no special
13461cb0ef41Sopenharmony_ci`'error'` event semantics and does not listen to the `'error'` event.
13471cb0ef41Sopenharmony_ci
13481cb0ef41Sopenharmony_ci```mjs
13491cb0ef41Sopenharmony_ciimport { once, EventEmitter } from 'node:events';
13501cb0ef41Sopenharmony_ciimport process from 'node:process';
13511cb0ef41Sopenharmony_ci
13521cb0ef41Sopenharmony_ciconst ee = new EventEmitter();
13531cb0ef41Sopenharmony_ci
13541cb0ef41Sopenharmony_ciprocess.nextTick(() => {
13551cb0ef41Sopenharmony_ci  ee.emit('myevent', 42);
13561cb0ef41Sopenharmony_ci});
13571cb0ef41Sopenharmony_ci
13581cb0ef41Sopenharmony_ciconst [value] = await once(ee, 'myevent');
13591cb0ef41Sopenharmony_ciconsole.log(value);
13601cb0ef41Sopenharmony_ci
13611cb0ef41Sopenharmony_ciconst err = new Error('kaboom');
13621cb0ef41Sopenharmony_ciprocess.nextTick(() => {
13631cb0ef41Sopenharmony_ci  ee.emit('error', err);
13641cb0ef41Sopenharmony_ci});
13651cb0ef41Sopenharmony_ci
13661cb0ef41Sopenharmony_citry {
13671cb0ef41Sopenharmony_ci  await once(ee, 'myevent');
13681cb0ef41Sopenharmony_ci} catch (err) {
13691cb0ef41Sopenharmony_ci  console.error('error happened', err);
13701cb0ef41Sopenharmony_ci}
13711cb0ef41Sopenharmony_ci```
13721cb0ef41Sopenharmony_ci
13731cb0ef41Sopenharmony_ci```cjs
13741cb0ef41Sopenharmony_ciconst { once, EventEmitter } = require('node:events');
13751cb0ef41Sopenharmony_ci
13761cb0ef41Sopenharmony_ciasync function run() {
13771cb0ef41Sopenharmony_ci  const ee = new EventEmitter();
13781cb0ef41Sopenharmony_ci
13791cb0ef41Sopenharmony_ci  process.nextTick(() => {
13801cb0ef41Sopenharmony_ci    ee.emit('myevent', 42);
13811cb0ef41Sopenharmony_ci  });
13821cb0ef41Sopenharmony_ci
13831cb0ef41Sopenharmony_ci  const [value] = await once(ee, 'myevent');
13841cb0ef41Sopenharmony_ci  console.log(value);
13851cb0ef41Sopenharmony_ci
13861cb0ef41Sopenharmony_ci  const err = new Error('kaboom');
13871cb0ef41Sopenharmony_ci  process.nextTick(() => {
13881cb0ef41Sopenharmony_ci    ee.emit('error', err);
13891cb0ef41Sopenharmony_ci  });
13901cb0ef41Sopenharmony_ci
13911cb0ef41Sopenharmony_ci  try {
13921cb0ef41Sopenharmony_ci    await once(ee, 'myevent');
13931cb0ef41Sopenharmony_ci  } catch (err) {
13941cb0ef41Sopenharmony_ci    console.error('error happened', err);
13951cb0ef41Sopenharmony_ci  }
13961cb0ef41Sopenharmony_ci}
13971cb0ef41Sopenharmony_ci
13981cb0ef41Sopenharmony_cirun();
13991cb0ef41Sopenharmony_ci```
14001cb0ef41Sopenharmony_ci
14011cb0ef41Sopenharmony_ciThe special handling of the `'error'` event is only used when `events.once()`
14021cb0ef41Sopenharmony_ciis used to wait for another event. If `events.once()` is used to wait for the
14031cb0ef41Sopenharmony_ci'`error'` event itself, then it is treated as any other kind of event without
14041cb0ef41Sopenharmony_cispecial handling:
14051cb0ef41Sopenharmony_ci
14061cb0ef41Sopenharmony_ci```mjs
14071cb0ef41Sopenharmony_ciimport { EventEmitter, once } from 'node:events';
14081cb0ef41Sopenharmony_ci
14091cb0ef41Sopenharmony_ciconst ee = new EventEmitter();
14101cb0ef41Sopenharmony_ci
14111cb0ef41Sopenharmony_cionce(ee, 'error')
14121cb0ef41Sopenharmony_ci  .then(([err]) => console.log('ok', err.message))
14131cb0ef41Sopenharmony_ci  .catch((err) => console.error('error', err.message));
14141cb0ef41Sopenharmony_ci
14151cb0ef41Sopenharmony_ciee.emit('error', new Error('boom'));
14161cb0ef41Sopenharmony_ci
14171cb0ef41Sopenharmony_ci// Prints: ok boom
14181cb0ef41Sopenharmony_ci```
14191cb0ef41Sopenharmony_ci
14201cb0ef41Sopenharmony_ci```cjs
14211cb0ef41Sopenharmony_ciconst { EventEmitter, once } = require('node:events');
14221cb0ef41Sopenharmony_ci
14231cb0ef41Sopenharmony_ciconst ee = new EventEmitter();
14241cb0ef41Sopenharmony_ci
14251cb0ef41Sopenharmony_cionce(ee, 'error')
14261cb0ef41Sopenharmony_ci  .then(([err]) => console.log('ok', err.message))
14271cb0ef41Sopenharmony_ci  .catch((err) => console.error('error', err.message));
14281cb0ef41Sopenharmony_ci
14291cb0ef41Sopenharmony_ciee.emit('error', new Error('boom'));
14301cb0ef41Sopenharmony_ci
14311cb0ef41Sopenharmony_ci// Prints: ok boom
14321cb0ef41Sopenharmony_ci```
14331cb0ef41Sopenharmony_ci
14341cb0ef41Sopenharmony_ciAn {AbortSignal} can be used to cancel waiting for the event:
14351cb0ef41Sopenharmony_ci
14361cb0ef41Sopenharmony_ci```mjs
14371cb0ef41Sopenharmony_ciimport { EventEmitter, once } from 'node:events';
14381cb0ef41Sopenharmony_ci
14391cb0ef41Sopenharmony_ciconst ee = new EventEmitter();
14401cb0ef41Sopenharmony_ciconst ac = new AbortController();
14411cb0ef41Sopenharmony_ci
14421cb0ef41Sopenharmony_ciasync function foo(emitter, event, signal) {
14431cb0ef41Sopenharmony_ci  try {
14441cb0ef41Sopenharmony_ci    await once(emitter, event, { signal });
14451cb0ef41Sopenharmony_ci    console.log('event emitted!');
14461cb0ef41Sopenharmony_ci  } catch (error) {
14471cb0ef41Sopenharmony_ci    if (error.name === 'AbortError') {
14481cb0ef41Sopenharmony_ci      console.error('Waiting for the event was canceled!');
14491cb0ef41Sopenharmony_ci    } else {
14501cb0ef41Sopenharmony_ci      console.error('There was an error', error.message);
14511cb0ef41Sopenharmony_ci    }
14521cb0ef41Sopenharmony_ci  }
14531cb0ef41Sopenharmony_ci}
14541cb0ef41Sopenharmony_ci
14551cb0ef41Sopenharmony_cifoo(ee, 'foo', ac.signal);
14561cb0ef41Sopenharmony_ciac.abort(); // Abort waiting for the event
14571cb0ef41Sopenharmony_ciee.emit('foo'); // Prints: Waiting for the event was canceled!
14581cb0ef41Sopenharmony_ci```
14591cb0ef41Sopenharmony_ci
14601cb0ef41Sopenharmony_ci```cjs
14611cb0ef41Sopenharmony_ciconst { EventEmitter, once } = require('node:events');
14621cb0ef41Sopenharmony_ci
14631cb0ef41Sopenharmony_ciconst ee = new EventEmitter();
14641cb0ef41Sopenharmony_ciconst ac = new AbortController();
14651cb0ef41Sopenharmony_ci
14661cb0ef41Sopenharmony_ciasync function foo(emitter, event, signal) {
14671cb0ef41Sopenharmony_ci  try {
14681cb0ef41Sopenharmony_ci    await once(emitter, event, { signal });
14691cb0ef41Sopenharmony_ci    console.log('event emitted!');
14701cb0ef41Sopenharmony_ci  } catch (error) {
14711cb0ef41Sopenharmony_ci    if (error.name === 'AbortError') {
14721cb0ef41Sopenharmony_ci      console.error('Waiting for the event was canceled!');
14731cb0ef41Sopenharmony_ci    } else {
14741cb0ef41Sopenharmony_ci      console.error('There was an error', error.message);
14751cb0ef41Sopenharmony_ci    }
14761cb0ef41Sopenharmony_ci  }
14771cb0ef41Sopenharmony_ci}
14781cb0ef41Sopenharmony_ci
14791cb0ef41Sopenharmony_cifoo(ee, 'foo', ac.signal);
14801cb0ef41Sopenharmony_ciac.abort(); // Abort waiting for the event
14811cb0ef41Sopenharmony_ciee.emit('foo'); // Prints: Waiting for the event was canceled!
14821cb0ef41Sopenharmony_ci```
14831cb0ef41Sopenharmony_ci
14841cb0ef41Sopenharmony_ci### Awaiting multiple events emitted on `process.nextTick()`
14851cb0ef41Sopenharmony_ci
14861cb0ef41Sopenharmony_ciThere is an edge case worth noting when using the `events.once()` function
14871cb0ef41Sopenharmony_cito await multiple events emitted on in the same batch of `process.nextTick()`
14881cb0ef41Sopenharmony_cioperations, or whenever multiple events are emitted synchronously. Specifically,
14891cb0ef41Sopenharmony_cibecause the `process.nextTick()` queue is drained before the `Promise` microtask
14901cb0ef41Sopenharmony_ciqueue, and because `EventEmitter` emits all events synchronously, it is possible
14911cb0ef41Sopenharmony_cifor `events.once()` to miss an event.
14921cb0ef41Sopenharmony_ci
14931cb0ef41Sopenharmony_ci```mjs
14941cb0ef41Sopenharmony_ciimport { EventEmitter, once } from 'node:events';
14951cb0ef41Sopenharmony_ciimport process from 'node:process';
14961cb0ef41Sopenharmony_ci
14971cb0ef41Sopenharmony_ciconst myEE = new EventEmitter();
14981cb0ef41Sopenharmony_ci
14991cb0ef41Sopenharmony_ciasync function foo() {
15001cb0ef41Sopenharmony_ci  await once(myEE, 'bar');
15011cb0ef41Sopenharmony_ci  console.log('bar');
15021cb0ef41Sopenharmony_ci
15031cb0ef41Sopenharmony_ci  // This Promise will never resolve because the 'foo' event will
15041cb0ef41Sopenharmony_ci  // have already been emitted before the Promise is created.
15051cb0ef41Sopenharmony_ci  await once(myEE, 'foo');
15061cb0ef41Sopenharmony_ci  console.log('foo');
15071cb0ef41Sopenharmony_ci}
15081cb0ef41Sopenharmony_ci
15091cb0ef41Sopenharmony_ciprocess.nextTick(() => {
15101cb0ef41Sopenharmony_ci  myEE.emit('bar');
15111cb0ef41Sopenharmony_ci  myEE.emit('foo');
15121cb0ef41Sopenharmony_ci});
15131cb0ef41Sopenharmony_ci
15141cb0ef41Sopenharmony_cifoo().then(() => console.log('done'));
15151cb0ef41Sopenharmony_ci```
15161cb0ef41Sopenharmony_ci
15171cb0ef41Sopenharmony_ci```cjs
15181cb0ef41Sopenharmony_ciconst { EventEmitter, once } = require('node:events');
15191cb0ef41Sopenharmony_ci
15201cb0ef41Sopenharmony_ciconst myEE = new EventEmitter();
15211cb0ef41Sopenharmony_ci
15221cb0ef41Sopenharmony_ciasync function foo() {
15231cb0ef41Sopenharmony_ci  await once(myEE, 'bar');
15241cb0ef41Sopenharmony_ci  console.log('bar');
15251cb0ef41Sopenharmony_ci
15261cb0ef41Sopenharmony_ci  // This Promise will never resolve because the 'foo' event will
15271cb0ef41Sopenharmony_ci  // have already been emitted before the Promise is created.
15281cb0ef41Sopenharmony_ci  await once(myEE, 'foo');
15291cb0ef41Sopenharmony_ci  console.log('foo');
15301cb0ef41Sopenharmony_ci}
15311cb0ef41Sopenharmony_ci
15321cb0ef41Sopenharmony_ciprocess.nextTick(() => {
15331cb0ef41Sopenharmony_ci  myEE.emit('bar');
15341cb0ef41Sopenharmony_ci  myEE.emit('foo');
15351cb0ef41Sopenharmony_ci});
15361cb0ef41Sopenharmony_ci
15371cb0ef41Sopenharmony_cifoo().then(() => console.log('done'));
15381cb0ef41Sopenharmony_ci```
15391cb0ef41Sopenharmony_ci
15401cb0ef41Sopenharmony_ciTo catch both events, create each of the Promises _before_ awaiting either
15411cb0ef41Sopenharmony_ciof them, then it becomes possible to use `Promise.all()`, `Promise.race()`,
15421cb0ef41Sopenharmony_cior `Promise.allSettled()`:
15431cb0ef41Sopenharmony_ci
15441cb0ef41Sopenharmony_ci```mjs
15451cb0ef41Sopenharmony_ciimport { EventEmitter, once } from 'node:events';
15461cb0ef41Sopenharmony_ciimport process from 'node:process';
15471cb0ef41Sopenharmony_ci
15481cb0ef41Sopenharmony_ciconst myEE = new EventEmitter();
15491cb0ef41Sopenharmony_ci
15501cb0ef41Sopenharmony_ciasync function foo() {
15511cb0ef41Sopenharmony_ci  await Promise.all([once(myEE, 'bar'), once(myEE, 'foo')]);
15521cb0ef41Sopenharmony_ci  console.log('foo', 'bar');
15531cb0ef41Sopenharmony_ci}
15541cb0ef41Sopenharmony_ci
15551cb0ef41Sopenharmony_ciprocess.nextTick(() => {
15561cb0ef41Sopenharmony_ci  myEE.emit('bar');
15571cb0ef41Sopenharmony_ci  myEE.emit('foo');
15581cb0ef41Sopenharmony_ci});
15591cb0ef41Sopenharmony_ci
15601cb0ef41Sopenharmony_cifoo().then(() => console.log('done'));
15611cb0ef41Sopenharmony_ci```
15621cb0ef41Sopenharmony_ci
15631cb0ef41Sopenharmony_ci```cjs
15641cb0ef41Sopenharmony_ciconst { EventEmitter, once } = require('node:events');
15651cb0ef41Sopenharmony_ci
15661cb0ef41Sopenharmony_ciconst myEE = new EventEmitter();
15671cb0ef41Sopenharmony_ci
15681cb0ef41Sopenharmony_ciasync function foo() {
15691cb0ef41Sopenharmony_ci  await Promise.all([once(myEE, 'bar'), once(myEE, 'foo')]);
15701cb0ef41Sopenharmony_ci  console.log('foo', 'bar');
15711cb0ef41Sopenharmony_ci}
15721cb0ef41Sopenharmony_ci
15731cb0ef41Sopenharmony_ciprocess.nextTick(() => {
15741cb0ef41Sopenharmony_ci  myEE.emit('bar');
15751cb0ef41Sopenharmony_ci  myEE.emit('foo');
15761cb0ef41Sopenharmony_ci});
15771cb0ef41Sopenharmony_ci
15781cb0ef41Sopenharmony_cifoo().then(() => console.log('done'));
15791cb0ef41Sopenharmony_ci```
15801cb0ef41Sopenharmony_ci
15811cb0ef41Sopenharmony_ci## `events.captureRejections`
15821cb0ef41Sopenharmony_ci
15831cb0ef41Sopenharmony_ci<!-- YAML
15841cb0ef41Sopenharmony_ciadded:
15851cb0ef41Sopenharmony_ci - v13.4.0
15861cb0ef41Sopenharmony_ci - v12.16.0
15871cb0ef41Sopenharmony_cichanges:
15881cb0ef41Sopenharmony_ci  - version:
15891cb0ef41Sopenharmony_ci    - v17.4.0
15901cb0ef41Sopenharmony_ci    - v16.14.0
15911cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/41267
15921cb0ef41Sopenharmony_ci    description: No longer experimental.
15931cb0ef41Sopenharmony_ci-->
15941cb0ef41Sopenharmony_ci
15951cb0ef41Sopenharmony_ciValue: {boolean}
15961cb0ef41Sopenharmony_ci
15971cb0ef41Sopenharmony_ciChange the default `captureRejections` option on all new `EventEmitter` objects.
15981cb0ef41Sopenharmony_ci
15991cb0ef41Sopenharmony_ci## `events.captureRejectionSymbol`
16001cb0ef41Sopenharmony_ci
16011cb0ef41Sopenharmony_ci<!-- YAML
16021cb0ef41Sopenharmony_ciadded:
16031cb0ef41Sopenharmony_ci  - v13.4.0
16041cb0ef41Sopenharmony_ci  - v12.16.0
16051cb0ef41Sopenharmony_cichanges:
16061cb0ef41Sopenharmony_ci  - version:
16071cb0ef41Sopenharmony_ci    - v17.4.0
16081cb0ef41Sopenharmony_ci    - v16.14.0
16091cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/41267
16101cb0ef41Sopenharmony_ci    description: No longer experimental.
16111cb0ef41Sopenharmony_ci-->
16121cb0ef41Sopenharmony_ci
16131cb0ef41Sopenharmony_ciValue: `Symbol.for('nodejs.rejection')`
16141cb0ef41Sopenharmony_ci
16151cb0ef41Sopenharmony_ciSee how to write a custom [rejection handler][rejection].
16161cb0ef41Sopenharmony_ci
16171cb0ef41Sopenharmony_ci## `events.listenerCount(emitter, eventName)`
16181cb0ef41Sopenharmony_ci
16191cb0ef41Sopenharmony_ci<!-- YAML
16201cb0ef41Sopenharmony_ciadded: v0.9.12
16211cb0ef41Sopenharmony_cideprecated: v3.2.0
16221cb0ef41Sopenharmony_ci-->
16231cb0ef41Sopenharmony_ci
16241cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated: Use [`emitter.listenerCount()`][] instead.
16251cb0ef41Sopenharmony_ci
16261cb0ef41Sopenharmony_ci* `emitter` {EventEmitter} The emitter to query
16271cb0ef41Sopenharmony_ci* `eventName` {string|symbol} The event name
16281cb0ef41Sopenharmony_ci
16291cb0ef41Sopenharmony_ciA class method that returns the number of listeners for the given `eventName`
16301cb0ef41Sopenharmony_ciregistered on the given `emitter`.
16311cb0ef41Sopenharmony_ci
16321cb0ef41Sopenharmony_ci```mjs
16331cb0ef41Sopenharmony_ciimport { EventEmitter, listenerCount } from 'node:events';
16341cb0ef41Sopenharmony_ci
16351cb0ef41Sopenharmony_ciconst myEmitter = new EventEmitter();
16361cb0ef41Sopenharmony_cimyEmitter.on('event', () => {});
16371cb0ef41Sopenharmony_cimyEmitter.on('event', () => {});
16381cb0ef41Sopenharmony_ciconsole.log(listenerCount(myEmitter, 'event'));
16391cb0ef41Sopenharmony_ci// Prints: 2
16401cb0ef41Sopenharmony_ci```
16411cb0ef41Sopenharmony_ci
16421cb0ef41Sopenharmony_ci```cjs
16431cb0ef41Sopenharmony_ciconst { EventEmitter, listenerCount } = require('node:events');
16441cb0ef41Sopenharmony_ci
16451cb0ef41Sopenharmony_ciconst myEmitter = new EventEmitter();
16461cb0ef41Sopenharmony_cimyEmitter.on('event', () => {});
16471cb0ef41Sopenharmony_cimyEmitter.on('event', () => {});
16481cb0ef41Sopenharmony_ciconsole.log(listenerCount(myEmitter, 'event'));
16491cb0ef41Sopenharmony_ci// Prints: 2
16501cb0ef41Sopenharmony_ci```
16511cb0ef41Sopenharmony_ci
16521cb0ef41Sopenharmony_ci## `events.on(emitter, eventName[, options])`
16531cb0ef41Sopenharmony_ci
16541cb0ef41Sopenharmony_ci<!-- YAML
16551cb0ef41Sopenharmony_ciadded:
16561cb0ef41Sopenharmony_ci - v13.6.0
16571cb0ef41Sopenharmony_ci - v12.16.0
16581cb0ef41Sopenharmony_ci-->
16591cb0ef41Sopenharmony_ci
16601cb0ef41Sopenharmony_ci* `emitter` {EventEmitter}
16611cb0ef41Sopenharmony_ci* `eventName` {string|symbol} The name of the event being listened for
16621cb0ef41Sopenharmony_ci* `options` {Object}
16631cb0ef41Sopenharmony_ci  * `signal` {AbortSignal} Can be used to cancel awaiting events.
16641cb0ef41Sopenharmony_ci* Returns: {AsyncIterator} that iterates `eventName` events emitted by the `emitter`
16651cb0ef41Sopenharmony_ci
16661cb0ef41Sopenharmony_ci```mjs
16671cb0ef41Sopenharmony_ciimport { on, EventEmitter } from 'node:events';
16681cb0ef41Sopenharmony_ciimport process from 'node:process';
16691cb0ef41Sopenharmony_ci
16701cb0ef41Sopenharmony_ciconst ee = new EventEmitter();
16711cb0ef41Sopenharmony_ci
16721cb0ef41Sopenharmony_ci// Emit later on
16731cb0ef41Sopenharmony_ciprocess.nextTick(() => {
16741cb0ef41Sopenharmony_ci  ee.emit('foo', 'bar');
16751cb0ef41Sopenharmony_ci  ee.emit('foo', 42);
16761cb0ef41Sopenharmony_ci});
16771cb0ef41Sopenharmony_ci
16781cb0ef41Sopenharmony_cifor await (const event of on(ee, 'foo')) {
16791cb0ef41Sopenharmony_ci  // The execution of this inner block is synchronous and it
16801cb0ef41Sopenharmony_ci  // processes one event at a time (even with await). Do not use
16811cb0ef41Sopenharmony_ci  // if concurrent execution is required.
16821cb0ef41Sopenharmony_ci  console.log(event); // prints ['bar'] [42]
16831cb0ef41Sopenharmony_ci}
16841cb0ef41Sopenharmony_ci// Unreachable here
16851cb0ef41Sopenharmony_ci```
16861cb0ef41Sopenharmony_ci
16871cb0ef41Sopenharmony_ci```cjs
16881cb0ef41Sopenharmony_ciconst { on, EventEmitter } = require('node:events');
16891cb0ef41Sopenharmony_ci
16901cb0ef41Sopenharmony_ci(async () => {
16911cb0ef41Sopenharmony_ci  const ee = new EventEmitter();
16921cb0ef41Sopenharmony_ci
16931cb0ef41Sopenharmony_ci  // Emit later on
16941cb0ef41Sopenharmony_ci  process.nextTick(() => {
16951cb0ef41Sopenharmony_ci    ee.emit('foo', 'bar');
16961cb0ef41Sopenharmony_ci    ee.emit('foo', 42);
16971cb0ef41Sopenharmony_ci  });
16981cb0ef41Sopenharmony_ci
16991cb0ef41Sopenharmony_ci  for await (const event of on(ee, 'foo')) {
17001cb0ef41Sopenharmony_ci    // The execution of this inner block is synchronous and it
17011cb0ef41Sopenharmony_ci    // processes one event at a time (even with await). Do not use
17021cb0ef41Sopenharmony_ci    // if concurrent execution is required.
17031cb0ef41Sopenharmony_ci    console.log(event); // prints ['bar'] [42]
17041cb0ef41Sopenharmony_ci  }
17051cb0ef41Sopenharmony_ci  // Unreachable here
17061cb0ef41Sopenharmony_ci})();
17071cb0ef41Sopenharmony_ci```
17081cb0ef41Sopenharmony_ci
17091cb0ef41Sopenharmony_ciReturns an `AsyncIterator` that iterates `eventName` events. It will throw
17101cb0ef41Sopenharmony_ciif the `EventEmitter` emits `'error'`. It removes all listeners when
17111cb0ef41Sopenharmony_ciexiting the loop. The `value` returned by each iteration is an array
17121cb0ef41Sopenharmony_cicomposed of the emitted event arguments.
17131cb0ef41Sopenharmony_ci
17141cb0ef41Sopenharmony_ciAn {AbortSignal} can be used to cancel waiting on events:
17151cb0ef41Sopenharmony_ci
17161cb0ef41Sopenharmony_ci```mjs
17171cb0ef41Sopenharmony_ciimport { on, EventEmitter } from 'node:events';
17181cb0ef41Sopenharmony_ciimport process from 'node:process';
17191cb0ef41Sopenharmony_ci
17201cb0ef41Sopenharmony_ciconst ac = new AbortController();
17211cb0ef41Sopenharmony_ci
17221cb0ef41Sopenharmony_ci(async () => {
17231cb0ef41Sopenharmony_ci  const ee = new EventEmitter();
17241cb0ef41Sopenharmony_ci
17251cb0ef41Sopenharmony_ci  // Emit later on
17261cb0ef41Sopenharmony_ci  process.nextTick(() => {
17271cb0ef41Sopenharmony_ci    ee.emit('foo', 'bar');
17281cb0ef41Sopenharmony_ci    ee.emit('foo', 42);
17291cb0ef41Sopenharmony_ci  });
17301cb0ef41Sopenharmony_ci
17311cb0ef41Sopenharmony_ci  for await (const event of on(ee, 'foo', { signal: ac.signal })) {
17321cb0ef41Sopenharmony_ci    // The execution of this inner block is synchronous and it
17331cb0ef41Sopenharmony_ci    // processes one event at a time (even with await). Do not use
17341cb0ef41Sopenharmony_ci    // if concurrent execution is required.
17351cb0ef41Sopenharmony_ci    console.log(event); // prints ['bar'] [42]
17361cb0ef41Sopenharmony_ci  }
17371cb0ef41Sopenharmony_ci  // Unreachable here
17381cb0ef41Sopenharmony_ci})();
17391cb0ef41Sopenharmony_ci
17401cb0ef41Sopenharmony_ciprocess.nextTick(() => ac.abort());
17411cb0ef41Sopenharmony_ci```
17421cb0ef41Sopenharmony_ci
17431cb0ef41Sopenharmony_ci```cjs
17441cb0ef41Sopenharmony_ciconst { on, EventEmitter } = require('node:events');
17451cb0ef41Sopenharmony_ci
17461cb0ef41Sopenharmony_ciconst ac = new AbortController();
17471cb0ef41Sopenharmony_ci
17481cb0ef41Sopenharmony_ci(async () => {
17491cb0ef41Sopenharmony_ci  const ee = new EventEmitter();
17501cb0ef41Sopenharmony_ci
17511cb0ef41Sopenharmony_ci  // Emit later on
17521cb0ef41Sopenharmony_ci  process.nextTick(() => {
17531cb0ef41Sopenharmony_ci    ee.emit('foo', 'bar');
17541cb0ef41Sopenharmony_ci    ee.emit('foo', 42);
17551cb0ef41Sopenharmony_ci  });
17561cb0ef41Sopenharmony_ci
17571cb0ef41Sopenharmony_ci  for await (const event of on(ee, 'foo', { signal: ac.signal })) {
17581cb0ef41Sopenharmony_ci    // The execution of this inner block is synchronous and it
17591cb0ef41Sopenharmony_ci    // processes one event at a time (even with await). Do not use
17601cb0ef41Sopenharmony_ci    // if concurrent execution is required.
17611cb0ef41Sopenharmony_ci    console.log(event); // prints ['bar'] [42]
17621cb0ef41Sopenharmony_ci  }
17631cb0ef41Sopenharmony_ci  // Unreachable here
17641cb0ef41Sopenharmony_ci})();
17651cb0ef41Sopenharmony_ci
17661cb0ef41Sopenharmony_ciprocess.nextTick(() => ac.abort());
17671cb0ef41Sopenharmony_ci```
17681cb0ef41Sopenharmony_ci
17691cb0ef41Sopenharmony_ci## `events.setMaxListeners(n[, ...eventTargets])`
17701cb0ef41Sopenharmony_ci
17711cb0ef41Sopenharmony_ci<!-- YAML
17721cb0ef41Sopenharmony_ciadded: v15.4.0
17731cb0ef41Sopenharmony_ci-->
17741cb0ef41Sopenharmony_ci
17751cb0ef41Sopenharmony_ci* `n` {number} A non-negative number. The maximum number of listeners per
17761cb0ef41Sopenharmony_ci  `EventTarget` event.
17771cb0ef41Sopenharmony_ci* `...eventsTargets` {EventTarget\[]|EventEmitter\[]} Zero or more {EventTarget}
17781cb0ef41Sopenharmony_ci  or {EventEmitter} instances. If none are specified, `n` is set as the default
17791cb0ef41Sopenharmony_ci  max for all newly created {EventTarget} and {EventEmitter} objects.
17801cb0ef41Sopenharmony_ci
17811cb0ef41Sopenharmony_ci```mjs
17821cb0ef41Sopenharmony_ciimport { setMaxListeners, EventEmitter } from 'node:events';
17831cb0ef41Sopenharmony_ci
17841cb0ef41Sopenharmony_ciconst target = new EventTarget();
17851cb0ef41Sopenharmony_ciconst emitter = new EventEmitter();
17861cb0ef41Sopenharmony_ci
17871cb0ef41Sopenharmony_cisetMaxListeners(5, target, emitter);
17881cb0ef41Sopenharmony_ci```
17891cb0ef41Sopenharmony_ci
17901cb0ef41Sopenharmony_ci```cjs
17911cb0ef41Sopenharmony_ciconst {
17921cb0ef41Sopenharmony_ci  setMaxListeners,
17931cb0ef41Sopenharmony_ci  EventEmitter,
17941cb0ef41Sopenharmony_ci} = require('node:events');
17951cb0ef41Sopenharmony_ci
17961cb0ef41Sopenharmony_ciconst target = new EventTarget();
17971cb0ef41Sopenharmony_ciconst emitter = new EventEmitter();
17981cb0ef41Sopenharmony_ci
17991cb0ef41Sopenharmony_cisetMaxListeners(5, target, emitter);
18001cb0ef41Sopenharmony_ci```
18011cb0ef41Sopenharmony_ci
18021cb0ef41Sopenharmony_ci## `events.addAbortListener(signal, resource)`
18031cb0ef41Sopenharmony_ci
18041cb0ef41Sopenharmony_ci<!-- YAML
18051cb0ef41Sopenharmony_ciadded: v18.18.0
18061cb0ef41Sopenharmony_ci-->
18071cb0ef41Sopenharmony_ci
18081cb0ef41Sopenharmony_ci> Stability: 1 - Experimental
18091cb0ef41Sopenharmony_ci
18101cb0ef41Sopenharmony_ci* `signal` {AbortSignal}
18111cb0ef41Sopenharmony_ci* `listener` {Function|EventListener}
18121cb0ef41Sopenharmony_ci* Returns: {Disposable} that removes the `abort` listener.
18131cb0ef41Sopenharmony_ci
18141cb0ef41Sopenharmony_ciListens once to the `abort` event on the provided `signal`.
18151cb0ef41Sopenharmony_ci
18161cb0ef41Sopenharmony_ciListening to the `abort` event on abort signals is unsafe and may
18171cb0ef41Sopenharmony_cilead to resource leaks since another third party with the signal can
18181cb0ef41Sopenharmony_cicall [`e.stopImmediatePropagation()`][]. Unfortunately Node.js cannot change
18191cb0ef41Sopenharmony_cithis since it would violate the web standard. Additionally, the original
18201cb0ef41Sopenharmony_ciAPI makes it easy to forget to remove listeners.
18211cb0ef41Sopenharmony_ci
18221cb0ef41Sopenharmony_ciThis API allows safely using `AbortSignal`s in Node.js APIs by solving these
18231cb0ef41Sopenharmony_citwo issues by listening to the event such that `stopImmediatePropagation` does
18241cb0ef41Sopenharmony_cinot prevent the listener from running.
18251cb0ef41Sopenharmony_ci
18261cb0ef41Sopenharmony_ciReturns a disposable so that it may be unsubscribed from more easily.
18271cb0ef41Sopenharmony_ci
18281cb0ef41Sopenharmony_ci```cjs
18291cb0ef41Sopenharmony_ciconst { addAbortListener } = require('node:events');
18301cb0ef41Sopenharmony_ci
18311cb0ef41Sopenharmony_cifunction example(signal) {
18321cb0ef41Sopenharmony_ci  let disposable;
18331cb0ef41Sopenharmony_ci  try {
18341cb0ef41Sopenharmony_ci    signal.addEventListener('abort', (e) => e.stopImmediatePropagation());
18351cb0ef41Sopenharmony_ci    disposable = addAbortListener(signal, (e) => {
18361cb0ef41Sopenharmony_ci      // Do something when signal is aborted.
18371cb0ef41Sopenharmony_ci    });
18381cb0ef41Sopenharmony_ci  } finally {
18391cb0ef41Sopenharmony_ci    disposable?.[Symbol.dispose]();
18401cb0ef41Sopenharmony_ci  }
18411cb0ef41Sopenharmony_ci}
18421cb0ef41Sopenharmony_ci```
18431cb0ef41Sopenharmony_ci
18441cb0ef41Sopenharmony_ci```mjs
18451cb0ef41Sopenharmony_ciimport { addAbortListener } from 'node:events';
18461cb0ef41Sopenharmony_ci
18471cb0ef41Sopenharmony_cifunction example(signal) {
18481cb0ef41Sopenharmony_ci  let disposable;
18491cb0ef41Sopenharmony_ci  try {
18501cb0ef41Sopenharmony_ci    signal.addEventListener('abort', (e) => e.stopImmediatePropagation());
18511cb0ef41Sopenharmony_ci    disposable = addAbortListener(signal, (e) => {
18521cb0ef41Sopenharmony_ci      // Do something when signal is aborted.
18531cb0ef41Sopenharmony_ci    });
18541cb0ef41Sopenharmony_ci  } finally {
18551cb0ef41Sopenharmony_ci    disposable?.[Symbol.dispose]();
18561cb0ef41Sopenharmony_ci  }
18571cb0ef41Sopenharmony_ci}
18581cb0ef41Sopenharmony_ci```
18591cb0ef41Sopenharmony_ci
18601cb0ef41Sopenharmony_ci## Class: `events.EventEmitterAsyncResource extends EventEmitter`
18611cb0ef41Sopenharmony_ci
18621cb0ef41Sopenharmony_ci<!-- YAML
18631cb0ef41Sopenharmony_ciadded:
18641cb0ef41Sopenharmony_ci  - v17.4.0
18651cb0ef41Sopenharmony_ci  - v16.14.0
18661cb0ef41Sopenharmony_ci-->
18671cb0ef41Sopenharmony_ci
18681cb0ef41Sopenharmony_ciIntegrates `EventEmitter` with {AsyncResource} for `EventEmitter`s that
18691cb0ef41Sopenharmony_cirequire manual async tracking. Specifically, all events emitted by instances
18701cb0ef41Sopenharmony_ciof `events.EventEmitterAsyncResource` will run within its [async context][].
18711cb0ef41Sopenharmony_ci
18721cb0ef41Sopenharmony_ci```mjs
18731cb0ef41Sopenharmony_ciimport { EventEmitterAsyncResource, EventEmitter } from 'node:events';
18741cb0ef41Sopenharmony_ciimport { notStrictEqual, strictEqual } from 'node:assert';
18751cb0ef41Sopenharmony_ciimport { executionAsyncId, triggerAsyncId } from 'node:async_hooks';
18761cb0ef41Sopenharmony_ci
18771cb0ef41Sopenharmony_ci// Async tracking tooling will identify this as 'Q'.
18781cb0ef41Sopenharmony_ciconst ee1 = new EventEmitterAsyncResource({ name: 'Q' });
18791cb0ef41Sopenharmony_ci
18801cb0ef41Sopenharmony_ci// 'foo' listeners will run in the EventEmitters async context.
18811cb0ef41Sopenharmony_ciee1.on('foo', () => {
18821cb0ef41Sopenharmony_ci  strictEqual(executionAsyncId(), ee1.asyncId);
18831cb0ef41Sopenharmony_ci  strictEqual(triggerAsyncId(), ee1.triggerAsyncId);
18841cb0ef41Sopenharmony_ci});
18851cb0ef41Sopenharmony_ci
18861cb0ef41Sopenharmony_ciconst ee2 = new EventEmitter();
18871cb0ef41Sopenharmony_ci
18881cb0ef41Sopenharmony_ci// 'foo' listeners on ordinary EventEmitters that do not track async
18891cb0ef41Sopenharmony_ci// context, however, run in the same async context as the emit().
18901cb0ef41Sopenharmony_ciee2.on('foo', () => {
18911cb0ef41Sopenharmony_ci  notStrictEqual(executionAsyncId(), ee2.asyncId);
18921cb0ef41Sopenharmony_ci  notStrictEqual(triggerAsyncId(), ee2.triggerAsyncId);
18931cb0ef41Sopenharmony_ci});
18941cb0ef41Sopenharmony_ci
18951cb0ef41Sopenharmony_ciPromise.resolve().then(() => {
18961cb0ef41Sopenharmony_ci  ee1.emit('foo');
18971cb0ef41Sopenharmony_ci  ee2.emit('foo');
18981cb0ef41Sopenharmony_ci});
18991cb0ef41Sopenharmony_ci```
19001cb0ef41Sopenharmony_ci
19011cb0ef41Sopenharmony_ci```cjs
19021cb0ef41Sopenharmony_ciconst { EventEmitterAsyncResource, EventEmitter } = require('node:events');
19031cb0ef41Sopenharmony_ciconst { notStrictEqual, strictEqual } = require('node:assert');
19041cb0ef41Sopenharmony_ciconst { executionAsyncId, triggerAsyncId } = require('node:async_hooks');
19051cb0ef41Sopenharmony_ci
19061cb0ef41Sopenharmony_ci// Async tracking tooling will identify this as 'Q'.
19071cb0ef41Sopenharmony_ciconst ee1 = new EventEmitterAsyncResource({ name: 'Q' });
19081cb0ef41Sopenharmony_ci
19091cb0ef41Sopenharmony_ci// 'foo' listeners will run in the EventEmitters async context.
19101cb0ef41Sopenharmony_ciee1.on('foo', () => {
19111cb0ef41Sopenharmony_ci  strictEqual(executionAsyncId(), ee1.asyncId);
19121cb0ef41Sopenharmony_ci  strictEqual(triggerAsyncId(), ee1.triggerAsyncId);
19131cb0ef41Sopenharmony_ci});
19141cb0ef41Sopenharmony_ci
19151cb0ef41Sopenharmony_ciconst ee2 = new EventEmitter();
19161cb0ef41Sopenharmony_ci
19171cb0ef41Sopenharmony_ci// 'foo' listeners on ordinary EventEmitters that do not track async
19181cb0ef41Sopenharmony_ci// context, however, run in the same async context as the emit().
19191cb0ef41Sopenharmony_ciee2.on('foo', () => {
19201cb0ef41Sopenharmony_ci  notStrictEqual(executionAsyncId(), ee2.asyncId);
19211cb0ef41Sopenharmony_ci  notStrictEqual(triggerAsyncId(), ee2.triggerAsyncId);
19221cb0ef41Sopenharmony_ci});
19231cb0ef41Sopenharmony_ci
19241cb0ef41Sopenharmony_ciPromise.resolve().then(() => {
19251cb0ef41Sopenharmony_ci  ee1.emit('foo');
19261cb0ef41Sopenharmony_ci  ee2.emit('foo');
19271cb0ef41Sopenharmony_ci});
19281cb0ef41Sopenharmony_ci```
19291cb0ef41Sopenharmony_ci
19301cb0ef41Sopenharmony_ciThe `EventEmitterAsyncResource` class has the same methods and takes the
19311cb0ef41Sopenharmony_cisame options as `EventEmitter` and `AsyncResource` themselves.
19321cb0ef41Sopenharmony_ci
19331cb0ef41Sopenharmony_ci### `new events.EventEmitterAsyncResource([options])`
19341cb0ef41Sopenharmony_ci
19351cb0ef41Sopenharmony_ci* `options` {Object}
19361cb0ef41Sopenharmony_ci  * `captureRejections` {boolean} It enables
19371cb0ef41Sopenharmony_ci    [automatic capturing of promise rejection][capturerejections].
19381cb0ef41Sopenharmony_ci    **Default:** `false`.
19391cb0ef41Sopenharmony_ci  * `name` {string} The type of async event. **Default:** [`new.target.name`][].
19401cb0ef41Sopenharmony_ci  * `triggerAsyncId` {number} The ID of the execution context that created this
19411cb0ef41Sopenharmony_ci    async event. **Default:** `executionAsyncId()`.
19421cb0ef41Sopenharmony_ci  * `requireManualDestroy` {boolean} If set to `true`, disables `emitDestroy`
19431cb0ef41Sopenharmony_ci    when the object is garbage collected. This usually does not need to be set
19441cb0ef41Sopenharmony_ci    (even if `emitDestroy` is called manually), unless the resource's `asyncId`
19451cb0ef41Sopenharmony_ci    is retrieved and the sensitive API's `emitDestroy` is called with it.
19461cb0ef41Sopenharmony_ci    When set to `false`, the `emitDestroy` call on garbage collection
19471cb0ef41Sopenharmony_ci    will only take place if there is at least one active `destroy` hook.
19481cb0ef41Sopenharmony_ci    **Default:** `false`.
19491cb0ef41Sopenharmony_ci
19501cb0ef41Sopenharmony_ci### `eventemitterasyncresource.asyncId`
19511cb0ef41Sopenharmony_ci
19521cb0ef41Sopenharmony_ci* Type: {number} The unique `asyncId` assigned to the resource.
19531cb0ef41Sopenharmony_ci
19541cb0ef41Sopenharmony_ci### `eventemitterasyncresource.asyncResource`
19551cb0ef41Sopenharmony_ci
19561cb0ef41Sopenharmony_ci* Type: The underlying {AsyncResource}.
19571cb0ef41Sopenharmony_ci
19581cb0ef41Sopenharmony_ciThe returned `AsyncResource` object has an additional `eventEmitter` property
19591cb0ef41Sopenharmony_cithat provides a reference to this `EventEmitterAsyncResource`.
19601cb0ef41Sopenharmony_ci
19611cb0ef41Sopenharmony_ci### `eventemitterasyncresource.emitDestroy()`
19621cb0ef41Sopenharmony_ci
19631cb0ef41Sopenharmony_ciCall all `destroy` hooks. This should only ever be called once. An error will
19641cb0ef41Sopenharmony_cibe thrown if it is called more than once. This **must** be manually called. If
19651cb0ef41Sopenharmony_cithe resource is left to be collected by the GC then the `destroy` hooks will
19661cb0ef41Sopenharmony_cinever be called.
19671cb0ef41Sopenharmony_ci
19681cb0ef41Sopenharmony_ci### `eventemitterasyncresource.triggerAsyncId`
19691cb0ef41Sopenharmony_ci
19701cb0ef41Sopenharmony_ci* Type: {number} The same `triggerAsyncId` that is passed to the
19711cb0ef41Sopenharmony_ci  `AsyncResource` constructor.
19721cb0ef41Sopenharmony_ci
19731cb0ef41Sopenharmony_ci<a id="event-target-and-event-api"></a>
19741cb0ef41Sopenharmony_ci
19751cb0ef41Sopenharmony_ci## `EventTarget` and `Event` API
19761cb0ef41Sopenharmony_ci
19771cb0ef41Sopenharmony_ci<!-- YAML
19781cb0ef41Sopenharmony_ciadded: v14.5.0
19791cb0ef41Sopenharmony_cichanges:
19801cb0ef41Sopenharmony_ci  - version: v16.0.0
19811cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/37237
19821cb0ef41Sopenharmony_ci    description: changed EventTarget error handling.
19831cb0ef41Sopenharmony_ci  - version: v15.4.0
19841cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/35949
19851cb0ef41Sopenharmony_ci    description: No longer experimental.
19861cb0ef41Sopenharmony_ci  - version: v15.0.0
19871cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/35496
19881cb0ef41Sopenharmony_ci    description:
19891cb0ef41Sopenharmony_ci      The `EventTarget` and `Event` classes are now available as globals.
19901cb0ef41Sopenharmony_ci-->
19911cb0ef41Sopenharmony_ci
19921cb0ef41Sopenharmony_ciThe `EventTarget` and `Event` objects are a Node.js-specific implementation
19931cb0ef41Sopenharmony_ciof the [`EventTarget` Web API][] that are exposed by some Node.js core APIs.
19941cb0ef41Sopenharmony_ci
19951cb0ef41Sopenharmony_ci```js
19961cb0ef41Sopenharmony_ciconst target = new EventTarget();
19971cb0ef41Sopenharmony_ci
19981cb0ef41Sopenharmony_citarget.addEventListener('foo', (event) => {
19991cb0ef41Sopenharmony_ci  console.log('foo event happened!');
20001cb0ef41Sopenharmony_ci});
20011cb0ef41Sopenharmony_ci```
20021cb0ef41Sopenharmony_ci
20031cb0ef41Sopenharmony_ci### Node.js `EventTarget` vs. DOM `EventTarget`
20041cb0ef41Sopenharmony_ci
20051cb0ef41Sopenharmony_ciThere are two key differences between the Node.js `EventTarget` and the
20061cb0ef41Sopenharmony_ci[`EventTarget` Web API][]:
20071cb0ef41Sopenharmony_ci
20081cb0ef41Sopenharmony_ci1. Whereas DOM `EventTarget` instances _may_ be hierarchical, there is no
20091cb0ef41Sopenharmony_ci   concept of hierarchy and event propagation in Node.js. That is, an event
20101cb0ef41Sopenharmony_ci   dispatched to an `EventTarget` does not propagate through a hierarchy of
20111cb0ef41Sopenharmony_ci   nested target objects that may each have their own set of handlers for the
20121cb0ef41Sopenharmony_ci   event.
20131cb0ef41Sopenharmony_ci2. In the Node.js `EventTarget`, if an event listener is an async function
20141cb0ef41Sopenharmony_ci   or returns a `Promise`, and the returned `Promise` rejects, the rejection
20151cb0ef41Sopenharmony_ci   is automatically captured and handled the same way as a listener that
20161cb0ef41Sopenharmony_ci   throws synchronously (see [`EventTarget` error handling][] for details).
20171cb0ef41Sopenharmony_ci
20181cb0ef41Sopenharmony_ci### `NodeEventTarget` vs. `EventEmitter`
20191cb0ef41Sopenharmony_ci
20201cb0ef41Sopenharmony_ciThe `NodeEventTarget` object implements a modified subset of the
20211cb0ef41Sopenharmony_ci`EventEmitter` API that allows it to closely _emulate_ an `EventEmitter` in
20221cb0ef41Sopenharmony_cicertain situations. A `NodeEventTarget` is _not_ an instance of `EventEmitter`
20231cb0ef41Sopenharmony_ciand cannot be used in place of an `EventEmitter` in most cases.
20241cb0ef41Sopenharmony_ci
20251cb0ef41Sopenharmony_ci1. Unlike `EventEmitter`, any given `listener` can be registered at most once
20261cb0ef41Sopenharmony_ci   per event `type`. Attempts to register a `listener` multiple times are
20271cb0ef41Sopenharmony_ci   ignored.
20281cb0ef41Sopenharmony_ci2. The `NodeEventTarget` does not emulate the full `EventEmitter` API.
20291cb0ef41Sopenharmony_ci   Specifically the `prependListener()`, `prependOnceListener()`,
20301cb0ef41Sopenharmony_ci   `rawListeners()`, and `errorMonitor` APIs are not emulated.
20311cb0ef41Sopenharmony_ci   The `'newListener'` and `'removeListener'` events will also not be emitted.
20321cb0ef41Sopenharmony_ci3. The `NodeEventTarget` does not implement any special default behavior
20331cb0ef41Sopenharmony_ci   for events with type `'error'`.
20341cb0ef41Sopenharmony_ci4. The `NodeEventTarget` supports `EventListener` objects as well as
20351cb0ef41Sopenharmony_ci   functions as handlers for all event types.
20361cb0ef41Sopenharmony_ci
20371cb0ef41Sopenharmony_ci### Event listener
20381cb0ef41Sopenharmony_ci
20391cb0ef41Sopenharmony_ciEvent listeners registered for an event `type` may either be JavaScript
20401cb0ef41Sopenharmony_cifunctions or objects with a `handleEvent` property whose value is a function.
20411cb0ef41Sopenharmony_ci
20421cb0ef41Sopenharmony_ciIn either case, the handler function is invoked with the `event` argument
20431cb0ef41Sopenharmony_cipassed to the `eventTarget.dispatchEvent()` function.
20441cb0ef41Sopenharmony_ci
20451cb0ef41Sopenharmony_ciAsync functions may be used as event listeners. If an async handler function
20461cb0ef41Sopenharmony_cirejects, the rejection is captured and handled as described in
20471cb0ef41Sopenharmony_ci[`EventTarget` error handling][].
20481cb0ef41Sopenharmony_ci
20491cb0ef41Sopenharmony_ciAn error thrown by one handler function does not prevent the other handlers
20501cb0ef41Sopenharmony_cifrom being invoked.
20511cb0ef41Sopenharmony_ci
20521cb0ef41Sopenharmony_ciThe return value of a handler function is ignored.
20531cb0ef41Sopenharmony_ci
20541cb0ef41Sopenharmony_ciHandlers are always invoked in the order they were added.
20551cb0ef41Sopenharmony_ci
20561cb0ef41Sopenharmony_ciHandler functions may mutate the `event` object.
20571cb0ef41Sopenharmony_ci
20581cb0ef41Sopenharmony_ci```js
20591cb0ef41Sopenharmony_cifunction handler1(event) {
20601cb0ef41Sopenharmony_ci  console.log(event.type);  // Prints 'foo'
20611cb0ef41Sopenharmony_ci  event.a = 1;
20621cb0ef41Sopenharmony_ci}
20631cb0ef41Sopenharmony_ci
20641cb0ef41Sopenharmony_ciasync function handler2(event) {
20651cb0ef41Sopenharmony_ci  console.log(event.type);  // Prints 'foo'
20661cb0ef41Sopenharmony_ci  console.log(event.a);  // Prints 1
20671cb0ef41Sopenharmony_ci}
20681cb0ef41Sopenharmony_ci
20691cb0ef41Sopenharmony_ciconst handler3 = {
20701cb0ef41Sopenharmony_ci  handleEvent(event) {
20711cb0ef41Sopenharmony_ci    console.log(event.type);  // Prints 'foo'
20721cb0ef41Sopenharmony_ci  },
20731cb0ef41Sopenharmony_ci};
20741cb0ef41Sopenharmony_ci
20751cb0ef41Sopenharmony_ciconst handler4 = {
20761cb0ef41Sopenharmony_ci  async handleEvent(event) {
20771cb0ef41Sopenharmony_ci    console.log(event.type);  // Prints 'foo'
20781cb0ef41Sopenharmony_ci  },
20791cb0ef41Sopenharmony_ci};
20801cb0ef41Sopenharmony_ci
20811cb0ef41Sopenharmony_ciconst target = new EventTarget();
20821cb0ef41Sopenharmony_ci
20831cb0ef41Sopenharmony_citarget.addEventListener('foo', handler1);
20841cb0ef41Sopenharmony_citarget.addEventListener('foo', handler2);
20851cb0ef41Sopenharmony_citarget.addEventListener('foo', handler3);
20861cb0ef41Sopenharmony_citarget.addEventListener('foo', handler4, { once: true });
20871cb0ef41Sopenharmony_ci```
20881cb0ef41Sopenharmony_ci
20891cb0ef41Sopenharmony_ci### `EventTarget` error handling
20901cb0ef41Sopenharmony_ci
20911cb0ef41Sopenharmony_ciWhen a registered event listener throws (or returns a Promise that rejects),
20921cb0ef41Sopenharmony_ciby default the error is treated as an uncaught exception on
20931cb0ef41Sopenharmony_ci`process.nextTick()`. This means uncaught exceptions in `EventTarget`s will
20941cb0ef41Sopenharmony_citerminate the Node.js process by default.
20951cb0ef41Sopenharmony_ci
20961cb0ef41Sopenharmony_ciThrowing within an event listener will _not_ stop the other registered handlers
20971cb0ef41Sopenharmony_cifrom being invoked.
20981cb0ef41Sopenharmony_ci
20991cb0ef41Sopenharmony_ciThe `EventTarget` does not implement any special default handling for `'error'`
21001cb0ef41Sopenharmony_citype events like `EventEmitter`.
21011cb0ef41Sopenharmony_ci
21021cb0ef41Sopenharmony_ciCurrently errors are first forwarded to the `process.on('error')` event
21031cb0ef41Sopenharmony_cibefore reaching `process.on('uncaughtException')`. This behavior is
21041cb0ef41Sopenharmony_cideprecated and will change in a future release to align `EventTarget` with
21051cb0ef41Sopenharmony_ciother Node.js APIs. Any code relying on the `process.on('error')` event should
21061cb0ef41Sopenharmony_cibe aligned with the new behavior.
21071cb0ef41Sopenharmony_ci
21081cb0ef41Sopenharmony_ci### Class: `Event`
21091cb0ef41Sopenharmony_ci
21101cb0ef41Sopenharmony_ci<!-- YAML
21111cb0ef41Sopenharmony_ciadded: v14.5.0
21121cb0ef41Sopenharmony_cichanges:
21131cb0ef41Sopenharmony_ci  - version: v15.0.0
21141cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/35496
21151cb0ef41Sopenharmony_ci    description: The `Event` class is now available through the global object.
21161cb0ef41Sopenharmony_ci-->
21171cb0ef41Sopenharmony_ci
21181cb0ef41Sopenharmony_ciThe `Event` object is an adaptation of the [`Event` Web API][]. Instances
21191cb0ef41Sopenharmony_ciare created internally by Node.js.
21201cb0ef41Sopenharmony_ci
21211cb0ef41Sopenharmony_ci#### `event.bubbles`
21221cb0ef41Sopenharmony_ci
21231cb0ef41Sopenharmony_ci<!-- YAML
21241cb0ef41Sopenharmony_ciadded: v14.5.0
21251cb0ef41Sopenharmony_ci-->
21261cb0ef41Sopenharmony_ci
21271cb0ef41Sopenharmony_ci* Type: {boolean} Always returns `false`.
21281cb0ef41Sopenharmony_ci
21291cb0ef41Sopenharmony_ciThis is not used in Node.js and is provided purely for completeness.
21301cb0ef41Sopenharmony_ci
21311cb0ef41Sopenharmony_ci#### `event.cancelBubble`
21321cb0ef41Sopenharmony_ci
21331cb0ef41Sopenharmony_ci<!-- YAML
21341cb0ef41Sopenharmony_ciadded: v14.5.0
21351cb0ef41Sopenharmony_ci-->
21361cb0ef41Sopenharmony_ci
21371cb0ef41Sopenharmony_ci> Stability: 3 - Legacy: Use [`event.stopPropagation()`][] instead.
21381cb0ef41Sopenharmony_ci
21391cb0ef41Sopenharmony_ci* Type: {boolean}
21401cb0ef41Sopenharmony_ci
21411cb0ef41Sopenharmony_ciAlias for `event.stopPropagation()` if set to `true`. This is not used
21421cb0ef41Sopenharmony_ciin Node.js and is provided purely for completeness.
21431cb0ef41Sopenharmony_ci
21441cb0ef41Sopenharmony_ci#### `event.cancelable`
21451cb0ef41Sopenharmony_ci
21461cb0ef41Sopenharmony_ci<!-- YAML
21471cb0ef41Sopenharmony_ciadded: v14.5.0
21481cb0ef41Sopenharmony_ci-->
21491cb0ef41Sopenharmony_ci
21501cb0ef41Sopenharmony_ci* Type: {boolean} True if the event was created with the `cancelable` option.
21511cb0ef41Sopenharmony_ci
21521cb0ef41Sopenharmony_ci#### `event.composed`
21531cb0ef41Sopenharmony_ci
21541cb0ef41Sopenharmony_ci<!-- YAML
21551cb0ef41Sopenharmony_ciadded: v14.5.0
21561cb0ef41Sopenharmony_ci-->
21571cb0ef41Sopenharmony_ci
21581cb0ef41Sopenharmony_ci* Type: {boolean} Always returns `false`.
21591cb0ef41Sopenharmony_ci
21601cb0ef41Sopenharmony_ciThis is not used in Node.js and is provided purely for completeness.
21611cb0ef41Sopenharmony_ci
21621cb0ef41Sopenharmony_ci#### `event.composedPath()`
21631cb0ef41Sopenharmony_ci
21641cb0ef41Sopenharmony_ci<!-- YAML
21651cb0ef41Sopenharmony_ciadded: v14.5.0
21661cb0ef41Sopenharmony_ci-->
21671cb0ef41Sopenharmony_ci
21681cb0ef41Sopenharmony_ciReturns an array containing the current `EventTarget` as the only entry or
21691cb0ef41Sopenharmony_ciempty if the event is not being dispatched. This is not used in
21701cb0ef41Sopenharmony_ciNode.js and is provided purely for completeness.
21711cb0ef41Sopenharmony_ci
21721cb0ef41Sopenharmony_ci#### `event.currentTarget`
21731cb0ef41Sopenharmony_ci
21741cb0ef41Sopenharmony_ci<!-- YAML
21751cb0ef41Sopenharmony_ciadded: v14.5.0
21761cb0ef41Sopenharmony_ci-->
21771cb0ef41Sopenharmony_ci
21781cb0ef41Sopenharmony_ci* Type: {EventTarget} The `EventTarget` dispatching the event.
21791cb0ef41Sopenharmony_ci
21801cb0ef41Sopenharmony_ciAlias for `event.target`.
21811cb0ef41Sopenharmony_ci
21821cb0ef41Sopenharmony_ci#### `event.defaultPrevented`
21831cb0ef41Sopenharmony_ci
21841cb0ef41Sopenharmony_ci<!-- YAML
21851cb0ef41Sopenharmony_ciadded: v14.5.0
21861cb0ef41Sopenharmony_ci-->
21871cb0ef41Sopenharmony_ci
21881cb0ef41Sopenharmony_ci* Type: {boolean}
21891cb0ef41Sopenharmony_ci
21901cb0ef41Sopenharmony_ciIs `true` if `cancelable` is `true` and `event.preventDefault()` has been
21911cb0ef41Sopenharmony_cicalled.
21921cb0ef41Sopenharmony_ci
21931cb0ef41Sopenharmony_ci#### `event.eventPhase`
21941cb0ef41Sopenharmony_ci
21951cb0ef41Sopenharmony_ci<!-- YAML
21961cb0ef41Sopenharmony_ciadded: v14.5.0
21971cb0ef41Sopenharmony_ci-->
21981cb0ef41Sopenharmony_ci
21991cb0ef41Sopenharmony_ci* Type: {number} Returns `0` while an event is not being dispatched, `2` while
22001cb0ef41Sopenharmony_ci  it is being dispatched.
22011cb0ef41Sopenharmony_ci
22021cb0ef41Sopenharmony_ciThis is not used in Node.js and is provided purely for completeness.
22031cb0ef41Sopenharmony_ci
22041cb0ef41Sopenharmony_ci#### `event.isTrusted`
22051cb0ef41Sopenharmony_ci
22061cb0ef41Sopenharmony_ci<!-- YAML
22071cb0ef41Sopenharmony_ciadded: v14.5.0
22081cb0ef41Sopenharmony_ci-->
22091cb0ef41Sopenharmony_ci
22101cb0ef41Sopenharmony_ci* Type: {boolean}
22111cb0ef41Sopenharmony_ci
22121cb0ef41Sopenharmony_ciThe {AbortSignal} `"abort"` event is emitted with `isTrusted` set to `true`. The
22131cb0ef41Sopenharmony_civalue is `false` in all other cases.
22141cb0ef41Sopenharmony_ci
22151cb0ef41Sopenharmony_ci#### `event.preventDefault()`
22161cb0ef41Sopenharmony_ci
22171cb0ef41Sopenharmony_ci<!-- YAML
22181cb0ef41Sopenharmony_ciadded: v14.5.0
22191cb0ef41Sopenharmony_ci-->
22201cb0ef41Sopenharmony_ci
22211cb0ef41Sopenharmony_ciSets the `defaultPrevented` property to `true` if `cancelable` is `true`.
22221cb0ef41Sopenharmony_ci
22231cb0ef41Sopenharmony_ci#### `event.returnValue`
22241cb0ef41Sopenharmony_ci
22251cb0ef41Sopenharmony_ci<!-- YAML
22261cb0ef41Sopenharmony_ciadded: v14.5.0
22271cb0ef41Sopenharmony_ci-->
22281cb0ef41Sopenharmony_ci
22291cb0ef41Sopenharmony_ci> Stability: 3 - Legacy: Use [`event.defaultPrevented`][] instead.
22301cb0ef41Sopenharmony_ci
22311cb0ef41Sopenharmony_ci* Type: {boolean} True if the event has not been canceled.
22321cb0ef41Sopenharmony_ci
22331cb0ef41Sopenharmony_ciThe value of `event.returnValue` is always the opposite of `event.defaultPrevented`.
22341cb0ef41Sopenharmony_ciThis is not used in Node.js and is provided purely for completeness.
22351cb0ef41Sopenharmony_ci
22361cb0ef41Sopenharmony_ci#### `event.srcElement`
22371cb0ef41Sopenharmony_ci
22381cb0ef41Sopenharmony_ci<!-- YAML
22391cb0ef41Sopenharmony_ciadded: v14.5.0
22401cb0ef41Sopenharmony_ci-->
22411cb0ef41Sopenharmony_ci
22421cb0ef41Sopenharmony_ci> Stability: 3 - Legacy: Use [`event.target`][] instead.
22431cb0ef41Sopenharmony_ci
22441cb0ef41Sopenharmony_ci* Type: {EventTarget} The `EventTarget` dispatching the event.
22451cb0ef41Sopenharmony_ci
22461cb0ef41Sopenharmony_ciAlias for `event.target`.
22471cb0ef41Sopenharmony_ci
22481cb0ef41Sopenharmony_ci#### `event.stopImmediatePropagation()`
22491cb0ef41Sopenharmony_ci
22501cb0ef41Sopenharmony_ci<!-- YAML
22511cb0ef41Sopenharmony_ciadded: v14.5.0
22521cb0ef41Sopenharmony_ci-->
22531cb0ef41Sopenharmony_ci
22541cb0ef41Sopenharmony_ciStops the invocation of event listeners after the current one completes.
22551cb0ef41Sopenharmony_ci
22561cb0ef41Sopenharmony_ci#### `event.stopPropagation()`
22571cb0ef41Sopenharmony_ci
22581cb0ef41Sopenharmony_ci<!-- YAML
22591cb0ef41Sopenharmony_ciadded: v14.5.0
22601cb0ef41Sopenharmony_ci-->
22611cb0ef41Sopenharmony_ci
22621cb0ef41Sopenharmony_ciThis is not used in Node.js and is provided purely for completeness.
22631cb0ef41Sopenharmony_ci
22641cb0ef41Sopenharmony_ci#### `event.target`
22651cb0ef41Sopenharmony_ci
22661cb0ef41Sopenharmony_ci<!-- YAML
22671cb0ef41Sopenharmony_ciadded: v14.5.0
22681cb0ef41Sopenharmony_ci-->
22691cb0ef41Sopenharmony_ci
22701cb0ef41Sopenharmony_ci* Type: {EventTarget} The `EventTarget` dispatching the event.
22711cb0ef41Sopenharmony_ci
22721cb0ef41Sopenharmony_ci#### `event.timeStamp`
22731cb0ef41Sopenharmony_ci
22741cb0ef41Sopenharmony_ci<!-- YAML
22751cb0ef41Sopenharmony_ciadded: v14.5.0
22761cb0ef41Sopenharmony_ci-->
22771cb0ef41Sopenharmony_ci
22781cb0ef41Sopenharmony_ci* Type: {number}
22791cb0ef41Sopenharmony_ci
22801cb0ef41Sopenharmony_ciThe millisecond timestamp when the `Event` object was created.
22811cb0ef41Sopenharmony_ci
22821cb0ef41Sopenharmony_ci#### `event.type`
22831cb0ef41Sopenharmony_ci
22841cb0ef41Sopenharmony_ci<!-- YAML
22851cb0ef41Sopenharmony_ciadded: v14.5.0
22861cb0ef41Sopenharmony_ci-->
22871cb0ef41Sopenharmony_ci
22881cb0ef41Sopenharmony_ci* Type: {string}
22891cb0ef41Sopenharmony_ci
22901cb0ef41Sopenharmony_ciThe event type identifier.
22911cb0ef41Sopenharmony_ci
22921cb0ef41Sopenharmony_ci### Class: `EventTarget`
22931cb0ef41Sopenharmony_ci
22941cb0ef41Sopenharmony_ci<!-- YAML
22951cb0ef41Sopenharmony_ciadded: v14.5.0
22961cb0ef41Sopenharmony_cichanges:
22971cb0ef41Sopenharmony_ci  - version: v15.0.0
22981cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/35496
22991cb0ef41Sopenharmony_ci    description:
23001cb0ef41Sopenharmony_ci      The `EventTarget` class is now available through the global object.
23011cb0ef41Sopenharmony_ci-->
23021cb0ef41Sopenharmony_ci
23031cb0ef41Sopenharmony_ci#### `eventTarget.addEventListener(type, listener[, options])`
23041cb0ef41Sopenharmony_ci
23051cb0ef41Sopenharmony_ci<!-- YAML
23061cb0ef41Sopenharmony_ciadded: v14.5.0
23071cb0ef41Sopenharmony_cichanges:
23081cb0ef41Sopenharmony_ci  - version: v15.4.0
23091cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/36258
23101cb0ef41Sopenharmony_ci    description: add support for `signal` option.
23111cb0ef41Sopenharmony_ci-->
23121cb0ef41Sopenharmony_ci
23131cb0ef41Sopenharmony_ci* `type` {string}
23141cb0ef41Sopenharmony_ci* `listener` {Function|EventListener}
23151cb0ef41Sopenharmony_ci* `options` {Object}
23161cb0ef41Sopenharmony_ci  * `once` {boolean} When `true`, the listener is automatically removed
23171cb0ef41Sopenharmony_ci    when it is first invoked. **Default:** `false`.
23181cb0ef41Sopenharmony_ci  * `passive` {boolean} When `true`, serves as a hint that the listener will
23191cb0ef41Sopenharmony_ci    not call the `Event` object's `preventDefault()` method.
23201cb0ef41Sopenharmony_ci    **Default:** `false`.
23211cb0ef41Sopenharmony_ci  * `capture` {boolean} Not directly used by Node.js. Added for API
23221cb0ef41Sopenharmony_ci    completeness. **Default:** `false`.
23231cb0ef41Sopenharmony_ci  * `signal` {AbortSignal} The listener will be removed when the given
23241cb0ef41Sopenharmony_ci    AbortSignal object's `abort()` method is called.
23251cb0ef41Sopenharmony_ci
23261cb0ef41Sopenharmony_ciAdds a new handler for the `type` event. Any given `listener` is added
23271cb0ef41Sopenharmony_cionly once per `type` and per `capture` option value.
23281cb0ef41Sopenharmony_ci
23291cb0ef41Sopenharmony_ciIf the `once` option is `true`, the `listener` is removed after the
23301cb0ef41Sopenharmony_cinext time a `type` event is dispatched.
23311cb0ef41Sopenharmony_ci
23321cb0ef41Sopenharmony_ciThe `capture` option is not used by Node.js in any functional way other than
23331cb0ef41Sopenharmony_citracking registered event listeners per the `EventTarget` specification.
23341cb0ef41Sopenharmony_ciSpecifically, the `capture` option is used as part of the key when registering
23351cb0ef41Sopenharmony_cia `listener`. Any individual `listener` may be added once with
23361cb0ef41Sopenharmony_ci`capture = false`, and once with `capture = true`.
23371cb0ef41Sopenharmony_ci
23381cb0ef41Sopenharmony_ci```js
23391cb0ef41Sopenharmony_cifunction handler(event) {}
23401cb0ef41Sopenharmony_ci
23411cb0ef41Sopenharmony_ciconst target = new EventTarget();
23421cb0ef41Sopenharmony_citarget.addEventListener('foo', handler, { capture: true });  // first
23431cb0ef41Sopenharmony_citarget.addEventListener('foo', handler, { capture: false }); // second
23441cb0ef41Sopenharmony_ci
23451cb0ef41Sopenharmony_ci// Removes the second instance of handler
23461cb0ef41Sopenharmony_citarget.removeEventListener('foo', handler);
23471cb0ef41Sopenharmony_ci
23481cb0ef41Sopenharmony_ci// Removes the first instance of handler
23491cb0ef41Sopenharmony_citarget.removeEventListener('foo', handler, { capture: true });
23501cb0ef41Sopenharmony_ci```
23511cb0ef41Sopenharmony_ci
23521cb0ef41Sopenharmony_ci#### `eventTarget.dispatchEvent(event)`
23531cb0ef41Sopenharmony_ci
23541cb0ef41Sopenharmony_ci<!-- YAML
23551cb0ef41Sopenharmony_ciadded: v14.5.0
23561cb0ef41Sopenharmony_ci-->
23571cb0ef41Sopenharmony_ci
23581cb0ef41Sopenharmony_ci* `event` {Event}
23591cb0ef41Sopenharmony_ci* Returns: {boolean} `true` if either event's `cancelable` attribute value is
23601cb0ef41Sopenharmony_ci  false or its `preventDefault()` method was not invoked, otherwise `false`.
23611cb0ef41Sopenharmony_ci
23621cb0ef41Sopenharmony_ciDispatches the `event` to the list of handlers for `event.type`.
23631cb0ef41Sopenharmony_ci
23641cb0ef41Sopenharmony_ciThe registered event listeners is synchronously invoked in the order they
23651cb0ef41Sopenharmony_ciwere registered.
23661cb0ef41Sopenharmony_ci
23671cb0ef41Sopenharmony_ci#### `eventTarget.removeEventListener(type, listener[, options])`
23681cb0ef41Sopenharmony_ci
23691cb0ef41Sopenharmony_ci<!-- YAML
23701cb0ef41Sopenharmony_ciadded: v14.5.0
23711cb0ef41Sopenharmony_ci-->
23721cb0ef41Sopenharmony_ci
23731cb0ef41Sopenharmony_ci* `type` {string}
23741cb0ef41Sopenharmony_ci* `listener` {Function|EventListener}
23751cb0ef41Sopenharmony_ci* `options` {Object}
23761cb0ef41Sopenharmony_ci  * `capture` {boolean}
23771cb0ef41Sopenharmony_ci
23781cb0ef41Sopenharmony_ciRemoves the `listener` from the list of handlers for event `type`.
23791cb0ef41Sopenharmony_ci
23801cb0ef41Sopenharmony_ci### Class: `CustomEvent`
23811cb0ef41Sopenharmony_ci
23821cb0ef41Sopenharmony_ci<!-- YAML
23831cb0ef41Sopenharmony_ciadded: v18.7.0
23841cb0ef41Sopenharmony_ci-->
23851cb0ef41Sopenharmony_ci
23861cb0ef41Sopenharmony_ci> Stability: 1 - Experimental.
23871cb0ef41Sopenharmony_ci
23881cb0ef41Sopenharmony_ci* Extends: {Event}
23891cb0ef41Sopenharmony_ci
23901cb0ef41Sopenharmony_ciThe `CustomEvent` object is an adaptation of the [`CustomEvent` Web API][].
23911cb0ef41Sopenharmony_ciInstances are created internally by Node.js.
23921cb0ef41Sopenharmony_ci
23931cb0ef41Sopenharmony_ci#### `event.detail`
23941cb0ef41Sopenharmony_ci
23951cb0ef41Sopenharmony_ci<!-- YAML
23961cb0ef41Sopenharmony_ciadded: v18.7.0
23971cb0ef41Sopenharmony_ci-->
23981cb0ef41Sopenharmony_ci
23991cb0ef41Sopenharmony_ci> Stability: 1 - Experimental.
24001cb0ef41Sopenharmony_ci
24011cb0ef41Sopenharmony_ci* Type: {any} Returns custom data passed when initializing.
24021cb0ef41Sopenharmony_ci
24031cb0ef41Sopenharmony_ciRead-only.
24041cb0ef41Sopenharmony_ci
24051cb0ef41Sopenharmony_ci### Class: `NodeEventTarget`
24061cb0ef41Sopenharmony_ci
24071cb0ef41Sopenharmony_ci<!-- YAML
24081cb0ef41Sopenharmony_ciadded: v14.5.0
24091cb0ef41Sopenharmony_ci-->
24101cb0ef41Sopenharmony_ci
24111cb0ef41Sopenharmony_ci* Extends: {EventTarget}
24121cb0ef41Sopenharmony_ci
24131cb0ef41Sopenharmony_ciThe `NodeEventTarget` is a Node.js-specific extension to `EventTarget`
24141cb0ef41Sopenharmony_cithat emulates a subset of the `EventEmitter` API.
24151cb0ef41Sopenharmony_ci
24161cb0ef41Sopenharmony_ci#### `nodeEventTarget.addListener(type, listener)`
24171cb0ef41Sopenharmony_ci
24181cb0ef41Sopenharmony_ci<!-- YAML
24191cb0ef41Sopenharmony_ciadded: v14.5.0
24201cb0ef41Sopenharmony_ci-->
24211cb0ef41Sopenharmony_ci
24221cb0ef41Sopenharmony_ci* `type` {string}
24231cb0ef41Sopenharmony_ci
24241cb0ef41Sopenharmony_ci* `listener` {Function|EventListener}
24251cb0ef41Sopenharmony_ci
24261cb0ef41Sopenharmony_ci* Returns: {EventTarget} this
24271cb0ef41Sopenharmony_ci
24281cb0ef41Sopenharmony_ciNode.js-specific extension to the `EventTarget` class that emulates the
24291cb0ef41Sopenharmony_ciequivalent `EventEmitter` API. The only difference between `addListener()` and
24301cb0ef41Sopenharmony_ci`addEventListener()` is that `addListener()` will return a reference to the
24311cb0ef41Sopenharmony_ci`EventTarget`.
24321cb0ef41Sopenharmony_ci
24331cb0ef41Sopenharmony_ci#### `nodeEventTarget.emit(type, arg)`
24341cb0ef41Sopenharmony_ci
24351cb0ef41Sopenharmony_ci<!-- YAML
24361cb0ef41Sopenharmony_ciadded: v15.2.0
24371cb0ef41Sopenharmony_ci-->
24381cb0ef41Sopenharmony_ci
24391cb0ef41Sopenharmony_ci* `type` {string}
24401cb0ef41Sopenharmony_ci* `arg` {any}
24411cb0ef41Sopenharmony_ci* Returns: {boolean} `true` if event listeners registered for the `type` exist,
24421cb0ef41Sopenharmony_ci  otherwise `false`.
24431cb0ef41Sopenharmony_ci
24441cb0ef41Sopenharmony_ciNode.js-specific extension to the `EventTarget` class that dispatches the
24451cb0ef41Sopenharmony_ci`arg` to the list of handlers for `type`.
24461cb0ef41Sopenharmony_ci
24471cb0ef41Sopenharmony_ci#### `nodeEventTarget.eventNames()`
24481cb0ef41Sopenharmony_ci
24491cb0ef41Sopenharmony_ci<!-- YAML
24501cb0ef41Sopenharmony_ciadded: v14.5.0
24511cb0ef41Sopenharmony_ci-->
24521cb0ef41Sopenharmony_ci
24531cb0ef41Sopenharmony_ci* Returns: {string\[]}
24541cb0ef41Sopenharmony_ci
24551cb0ef41Sopenharmony_ciNode.js-specific extension to the `EventTarget` class that returns an array
24561cb0ef41Sopenharmony_ciof event `type` names for which event listeners are registered.
24571cb0ef41Sopenharmony_ci
24581cb0ef41Sopenharmony_ci#### `nodeEventTarget.listenerCount(type)`
24591cb0ef41Sopenharmony_ci
24601cb0ef41Sopenharmony_ci<!-- YAML
24611cb0ef41Sopenharmony_ciadded: v14.5.0
24621cb0ef41Sopenharmony_ci-->
24631cb0ef41Sopenharmony_ci
24641cb0ef41Sopenharmony_ci* `type` {string}
24651cb0ef41Sopenharmony_ci
24661cb0ef41Sopenharmony_ci* Returns: {number}
24671cb0ef41Sopenharmony_ci
24681cb0ef41Sopenharmony_ciNode.js-specific extension to the `EventTarget` class that returns the number
24691cb0ef41Sopenharmony_ciof event listeners registered for the `type`.
24701cb0ef41Sopenharmony_ci
24711cb0ef41Sopenharmony_ci#### `nodeEventTarget.setMaxListeners(n)`
24721cb0ef41Sopenharmony_ci
24731cb0ef41Sopenharmony_ci<!-- YAML
24741cb0ef41Sopenharmony_ciadded: v14.5.0
24751cb0ef41Sopenharmony_ci-->
24761cb0ef41Sopenharmony_ci
24771cb0ef41Sopenharmony_ci* `n` {number}
24781cb0ef41Sopenharmony_ci
24791cb0ef41Sopenharmony_ciNode.js-specific extension to the `EventTarget` class that sets the number
24801cb0ef41Sopenharmony_ciof max event listeners as `n`.
24811cb0ef41Sopenharmony_ci
24821cb0ef41Sopenharmony_ci#### `nodeEventTarget.getMaxListeners()`
24831cb0ef41Sopenharmony_ci
24841cb0ef41Sopenharmony_ci<!-- YAML
24851cb0ef41Sopenharmony_ciadded: v14.5.0
24861cb0ef41Sopenharmony_ci-->
24871cb0ef41Sopenharmony_ci
24881cb0ef41Sopenharmony_ci* Returns: {number}
24891cb0ef41Sopenharmony_ci
24901cb0ef41Sopenharmony_ciNode.js-specific extension to the `EventTarget` class that returns the number
24911cb0ef41Sopenharmony_ciof max event listeners.
24921cb0ef41Sopenharmony_ci
24931cb0ef41Sopenharmony_ci#### `nodeEventTarget.off(type, listener[, options])`
24941cb0ef41Sopenharmony_ci
24951cb0ef41Sopenharmony_ci<!-- YAML
24961cb0ef41Sopenharmony_ciadded: v14.5.0
24971cb0ef41Sopenharmony_ci-->
24981cb0ef41Sopenharmony_ci
24991cb0ef41Sopenharmony_ci* `type` {string}
25001cb0ef41Sopenharmony_ci
25011cb0ef41Sopenharmony_ci* `listener` {Function|EventListener}
25021cb0ef41Sopenharmony_ci
25031cb0ef41Sopenharmony_ci* `options` {Object}
25041cb0ef41Sopenharmony_ci  * `capture` {boolean}
25051cb0ef41Sopenharmony_ci
25061cb0ef41Sopenharmony_ci* Returns: {EventTarget} this
25071cb0ef41Sopenharmony_ci
25081cb0ef41Sopenharmony_ciNode.js-specific alias for `eventTarget.removeEventListener()`.
25091cb0ef41Sopenharmony_ci
25101cb0ef41Sopenharmony_ci#### `nodeEventTarget.on(type, listener)`
25111cb0ef41Sopenharmony_ci
25121cb0ef41Sopenharmony_ci<!-- YAML
25131cb0ef41Sopenharmony_ciadded: v14.5.0
25141cb0ef41Sopenharmony_ci-->
25151cb0ef41Sopenharmony_ci
25161cb0ef41Sopenharmony_ci* `type` {string}
25171cb0ef41Sopenharmony_ci
25181cb0ef41Sopenharmony_ci* `listener` {Function|EventListener}
25191cb0ef41Sopenharmony_ci
25201cb0ef41Sopenharmony_ci* Returns: {EventTarget} this
25211cb0ef41Sopenharmony_ci
25221cb0ef41Sopenharmony_ciNode.js-specific alias for `eventTarget.addEventListener()`.
25231cb0ef41Sopenharmony_ci
25241cb0ef41Sopenharmony_ci#### `nodeEventTarget.once(type, listener)`
25251cb0ef41Sopenharmony_ci
25261cb0ef41Sopenharmony_ci<!-- YAML
25271cb0ef41Sopenharmony_ciadded: v14.5.0
25281cb0ef41Sopenharmony_ci-->
25291cb0ef41Sopenharmony_ci
25301cb0ef41Sopenharmony_ci* `type` {string}
25311cb0ef41Sopenharmony_ci
25321cb0ef41Sopenharmony_ci* `listener` {Function|EventListener}
25331cb0ef41Sopenharmony_ci
25341cb0ef41Sopenharmony_ci* Returns: {EventTarget} this
25351cb0ef41Sopenharmony_ci
25361cb0ef41Sopenharmony_ciNode.js-specific extension to the `EventTarget` class that adds a `once`
25371cb0ef41Sopenharmony_cilistener for the given event `type`. This is equivalent to calling `on`
25381cb0ef41Sopenharmony_ciwith the `once` option set to `true`.
25391cb0ef41Sopenharmony_ci
25401cb0ef41Sopenharmony_ci#### `nodeEventTarget.removeAllListeners([type])`
25411cb0ef41Sopenharmony_ci
25421cb0ef41Sopenharmony_ci<!-- YAML
25431cb0ef41Sopenharmony_ciadded: v14.5.0
25441cb0ef41Sopenharmony_ci-->
25451cb0ef41Sopenharmony_ci
25461cb0ef41Sopenharmony_ci* `type` {string}
25471cb0ef41Sopenharmony_ci
25481cb0ef41Sopenharmony_ci* Returns: {EventTarget} this
25491cb0ef41Sopenharmony_ci
25501cb0ef41Sopenharmony_ciNode.js-specific extension to the `EventTarget` class. If `type` is specified,
25511cb0ef41Sopenharmony_ciremoves all registered listeners for `type`, otherwise removes all registered
25521cb0ef41Sopenharmony_cilisteners.
25531cb0ef41Sopenharmony_ci
25541cb0ef41Sopenharmony_ci#### `nodeEventTarget.removeListener(type, listener[, options])`
25551cb0ef41Sopenharmony_ci
25561cb0ef41Sopenharmony_ci<!-- YAML
25571cb0ef41Sopenharmony_ciadded: v14.5.0
25581cb0ef41Sopenharmony_ci-->
25591cb0ef41Sopenharmony_ci
25601cb0ef41Sopenharmony_ci* `type` {string}
25611cb0ef41Sopenharmony_ci
25621cb0ef41Sopenharmony_ci* `listener` {Function|EventListener}
25631cb0ef41Sopenharmony_ci
25641cb0ef41Sopenharmony_ci* `options` {Object}
25651cb0ef41Sopenharmony_ci  * `capture` {boolean}
25661cb0ef41Sopenharmony_ci
25671cb0ef41Sopenharmony_ci* Returns: {EventTarget} this
25681cb0ef41Sopenharmony_ci
25691cb0ef41Sopenharmony_ciNode.js-specific extension to the `EventTarget` class that removes the
25701cb0ef41Sopenharmony_ci`listener` for the given `type`. The only difference between `removeListener()`
25711cb0ef41Sopenharmony_ciand `removeEventListener()` is that `removeListener()` will return a reference
25721cb0ef41Sopenharmony_cito the `EventTarget`.
25731cb0ef41Sopenharmony_ci
25741cb0ef41Sopenharmony_ci[WHATWG-EventTarget]: https://dom.spec.whatwg.org/#interface-eventtarget
25751cb0ef41Sopenharmony_ci[`--trace-warnings`]: cli.md#--trace-warnings
25761cb0ef41Sopenharmony_ci[`CustomEvent` Web API]: https://dom.spec.whatwg.org/#customevent
25771cb0ef41Sopenharmony_ci[`EventTarget` Web API]: https://dom.spec.whatwg.org/#eventtarget
25781cb0ef41Sopenharmony_ci[`EventTarget` error handling]: #eventtarget-error-handling
25791cb0ef41Sopenharmony_ci[`Event` Web API]: https://dom.spec.whatwg.org/#event
25801cb0ef41Sopenharmony_ci[`domain`]: domain.md
25811cb0ef41Sopenharmony_ci[`e.stopImmediatePropagation()`]: #eventstopimmediatepropagation
25821cb0ef41Sopenharmony_ci[`emitter.listenerCount()`]: #emitterlistenercounteventname-listener
25831cb0ef41Sopenharmony_ci[`emitter.removeListener()`]: #emitterremovelistenereventname-listener
25841cb0ef41Sopenharmony_ci[`emitter.setMaxListeners(n)`]: #emittersetmaxlistenersn
25851cb0ef41Sopenharmony_ci[`event.defaultPrevented`]: #eventdefaultprevented
25861cb0ef41Sopenharmony_ci[`event.stopPropagation()`]: #eventstoppropagation
25871cb0ef41Sopenharmony_ci[`event.target`]: #eventtarget
25881cb0ef41Sopenharmony_ci[`events.defaultMaxListeners`]: #eventsdefaultmaxlisteners
25891cb0ef41Sopenharmony_ci[`fs.ReadStream`]: fs.md#class-fsreadstream
25901cb0ef41Sopenharmony_ci[`net.Server`]: net.md#class-netserver
25911cb0ef41Sopenharmony_ci[`new.target.name`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new.target
25921cb0ef41Sopenharmony_ci[`process.on('warning')`]: process.md#event-warning
25931cb0ef41Sopenharmony_ci[async context]: async_context.md
25941cb0ef41Sopenharmony_ci[capturerejections]: #capture-rejections-of-promises
25951cb0ef41Sopenharmony_ci[error]: #error-events
25961cb0ef41Sopenharmony_ci[rejection]: #emittersymbolfornodejsrejectionerr-eventname-args
25971cb0ef41Sopenharmony_ci[rejectionsymbol]: #eventscapturerejectionsymbol
25981cb0ef41Sopenharmony_ci[stream]: stream.md
2599