• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v14.20.1">
7  <title>Events | Node.js v14.20.1 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/events.html">
12</head>
13<body class="alt apidoc" id="api-section-events">
14  <div id="content" class="clearfix">
15    <div id="column2" class="interior">
16      <div id="intro" class="interior">
17        <a href="/" title="Go back to the home page">
18          Node.js
19        </a>
20      </div>
21      <ul>
22<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
23<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
24</ul>
25<hr class="line">
26<ul>
27<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
28<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
29<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
30<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
31<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
32<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
33<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
34<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
35<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
36<li><a href="console.html" class="nav-console">Console</a></li>
37<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
38<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
39<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
40<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
41<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
42<li><a href="dns.html" class="nav-dns">DNS</a></li>
43<li><a href="domain.html" class="nav-domain">Domain</a></li>
44<li><a href="errors.html" class="nav-errors">Errors</a></li>
45<li><a href="events.html" class="nav-events active">Events</a></li>
46<li><a href="fs.html" class="nav-fs">File system</a></li>
47<li><a href="globals.html" class="nav-globals">Globals</a></li>
48<li><a href="http.html" class="nav-http">HTTP</a></li>
49<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
50<li><a href="https.html" class="nav-https">HTTPS</a></li>
51<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
52<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
53<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
54<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
55<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li>
56<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
57<li><a href="net.html" class="nav-net">Net</a></li>
58<li><a href="os.html" class="nav-os">OS</a></li>
59<li><a href="path.html" class="nav-path">Path</a></li>
60<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
61<li><a href="policy.html" class="nav-policy">Policies</a></li>
62<li><a href="process.html" class="nav-process">Process</a></li>
63<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
64<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
65<li><a href="readline.html" class="nav-readline">Readline</a></li>
66<li><a href="repl.html" class="nav-repl">REPL</a></li>
67<li><a href="report.html" class="nav-report">Report</a></li>
68<li><a href="stream.html" class="nav-stream">Stream</a></li>
69<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
70<li><a href="timers.html" class="nav-timers">Timers</a></li>
71<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
72<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
73<li><a href="tty.html" class="nav-tty">TTY</a></li>
74<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
75<li><a href="url.html" class="nav-url">URL</a></li>
76<li><a href="util.html" class="nav-util">Utilities</a></li>
77<li><a href="v8.html" class="nav-v8">V8</a></li>
78<li><a href="vm.html" class="nav-vm">VM</a></li>
79<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
80<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
81<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
82</ul>
83<hr class="line">
84<ul>
85<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
86</ul>
87    </div>
88
89    <div id="column1" data-id="events" class="interior">
90      <header>
91        <div class="header-container">
92          <h1>Node.js v14.20.1 documentation</h1>
93          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
94            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
95              <path fill="none" d="M0 0h24v24H0z" />
96              <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
97              <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
98            </svg>
99            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
100              <path d="M0 0h24v24H0z" fill="none" />
101              <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
102            </svg>
103          </button>
104        </div>
105        <div id="gtoc">
106          <ul>
107            <li>
108              <a href="index.html">Index</a>
109            </li>
110            <li>
111              <a href="all.html">View on single page</a>
112            </li>
113            <li>
114              <a href="events.json">View as JSON</a>
115            </li>
116
117    <li class="version-picker">
118      <a href="#">View another version <span>&#x25bc;</span></a>
119      <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v18.x/api/events.html">18.x</a></li>
120<li><a href="https://nodejs.org/docs/latest-v17.x/api/events.html">17.x</a></li>
121<li><a href="https://nodejs.org/docs/latest-v16.x/api/events.html">16.x <b>LTS</b></a></li>
122<li><a href="https://nodejs.org/docs/latest-v15.x/api/events.html">15.x</a></li>
123<li><a href="https://nodejs.org/docs/latest-v14.x/api/events.html">14.x <b>LTS</b></a></li>
124<li><a href="https://nodejs.org/docs/latest-v13.x/api/events.html">13.x</a></li>
125<li><a href="https://nodejs.org/docs/latest-v12.x/api/events.html">12.x <b>LTS</b></a></li>
126<li><a href="https://nodejs.org/docs/latest-v11.x/api/events.html">11.x</a></li>
127<li><a href="https://nodejs.org/docs/latest-v10.x/api/events.html">10.x</a></li>
128<li><a href="https://nodejs.org/docs/latest-v9.x/api/events.html">9.x</a></li>
129<li><a href="https://nodejs.org/docs/latest-v8.x/api/events.html">8.x</a></li>
130<li><a href="https://nodejs.org/docs/latest-v7.x/api/events.html">7.x</a></li>
131<li><a href="https://nodejs.org/docs/latest-v6.x/api/events.html">6.x</a></li>
132<li><a href="https://nodejs.org/docs/latest-v5.x/api/events.html">5.x</a></li>
133<li><a href="https://nodejs.org/docs/latest-v4.x/api/events.html">4.x</a></li>
134<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/events.html">0.12.x</a></li>
135<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/events.html">0.10.x</a></li></ol>
136    </li>
137
138            <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/events.md">Edit on GitHub</a></li>
139          </ul>
140        </div>
141        <hr>
142      </header>
143
144      <details id="toc" open><summary>Table of contents</summary><ul>
145<li><span class="stability_2"><a href="#events_events">Events</a></span>
146<ul>
147<li><a href="#events_passing_arguments_and_this_to_listeners">Passing arguments and <code>this</code> to listeners</a></li>
148<li><a href="#events_asynchronous_vs_synchronous">Asynchronous vs. synchronous</a></li>
149<li><a href="#events_handling_events_only_once">Handling events only once</a></li>
150<li><a href="#events_error_events">Error events</a></li>
151<li><span class="stability_1"><a href="#events_capture_rejections_of_promises">Capture rejections of promises</a></span></li>
152<li><a href="#events_class_eventemitter">Class: <code>EventEmitter</code></a>
153<ul>
154<li><a href="#events_event_newlistener">Event: <code>'newListener'</code></a></li>
155<li><a href="#events_event_removelistener">Event: <code>'removeListener'</code></a></li>
156<li><a href="#events_emitter_addlistener_eventname_listener"><code>emitter.addListener(eventName, listener)</code></a></li>
157<li><a href="#events_emitter_emit_eventname_args"><code>emitter.emit(eventName[, ...args])</code></a></li>
158<li><a href="#events_emitter_eventnames"><code>emitter.eventNames()</code></a></li>
159<li><a href="#events_emitter_getmaxlisteners"><code>emitter.getMaxListeners()</code></a></li>
160<li><a href="#events_emitter_listenercount_eventname"><code>emitter.listenerCount(eventName)</code></a></li>
161<li><a href="#events_emitter_listeners_eventname"><code>emitter.listeners(eventName)</code></a></li>
162<li><a href="#events_emitter_off_eventname_listener"><code>emitter.off(eventName, listener)</code></a></li>
163<li><a href="#events_emitter_on_eventname_listener"><code>emitter.on(eventName, listener)</code></a></li>
164<li><a href="#events_emitter_once_eventname_listener"><code>emitter.once(eventName, listener)</code></a></li>
165<li><a href="#events_emitter_prependlistener_eventname_listener"><code>emitter.prependListener(eventName, listener)</code></a></li>
166<li><a href="#events_emitter_prependoncelistener_eventname_listener"><code>emitter.prependOnceListener(eventName, listener)</code></a></li>
167<li><a href="#events_emitter_removealllisteners_eventname"><code>emitter.removeAllListeners([eventName])</code></a></li>
168<li><a href="#events_emitter_removelistener_eventname_listener"><code>emitter.removeListener(eventName, listener)</code></a></li>
169<li><a href="#events_emitter_setmaxlisteners_n"><code>emitter.setMaxListeners(n)</code></a></li>
170<li><a href="#events_emitter_rawlisteners_eventname"><code>emitter.rawListeners(eventName)</code></a></li>
171<li><span class="stability_1"><a href="#events_emitter_symbol_for_nodejs_rejection_err_eventname_args"><code>emitter[Symbol.for('nodejs.rejection')](err, eventName[, ...args])</code></a></span></li>
172</ul>
173</li>
174<li><a href="#events_events_defaultmaxlisteners"><code>events.defaultMaxListeners</code></a></li>
175<li><a href="#events_events_errormonitor"><code>events.errorMonitor</code></a></li>
176<li><a href="#events_events_geteventlisteners_emitterortarget_eventname"><code>events.getEventListeners(emitterOrTarget, eventName)</code></a></li>
177<li><a href="#events_events_once_emitter_name_options"><code>events.once(emitter, name[, options])</code></a>
178<ul>
179<li><a href="#events_awaiting_multiple_events_emitted_on_process_nexttick">Awaiting multiple events emitted on <code>process.nextTick()</code></a></li>
180</ul>
181</li>
182<li><span class="stability_1"><a href="#events_events_capturerejections"><code>events.captureRejections</code></a></span></li>
183<li><span class="stability_1"><a href="#events_events_capturerejectionsymbol"><code>events.captureRejectionSymbol</code></a></span></li>
184<li><span class="stability_0"><a href="#events_events_listenercount_emitter_eventname"><code>events.listenerCount(emitter, eventName)</code></a></span></li>
185<li><a href="#events_events_on_emitter_eventname_options"><code>events.on(emitter, eventName[, options])</code></a></li>
186<li><a href="#events_events_setmaxlisteners_n_eventtargets"><code>events.setMaxListeners(n[, ...eventTargets])</code></a></li>
187<li><span class="stability_1"><a href="#events_eventtarget_and_event_api"><code>EventTarget</code> and <code>Event</code> API</a></span>
188<ul>
189<li><a href="#events_node_js_eventtarget_vs_dom_eventtarget">Node.js <code>EventTarget</code> vs. DOM <code>EventTarget</code></a></li>
190<li><a href="#events_nodeeventtarget_vs_eventemitter"><code>NodeEventTarget</code> vs. <code>EventEmitter</code></a></li>
191<li><a href="#events_event_listener">Event listener</a></li>
192<li><a href="#events_eventtarget_error_handling"><code>EventTarget</code> error handling</a></li>
193<li><a href="#events_class_event">Class: <code>Event</code></a>
194<ul>
195<li><a href="#events_event_bubbles"><code>event.bubbles</code></a></li>
196<li><a href="#events_event_cancelbubble"><code>event.cancelBubble()</code></a></li>
197<li><a href="#events_event_cancelable"><code>event.cancelable</code></a></li>
198<li><a href="#events_event_composed"><code>event.composed</code></a></li>
199<li><a href="#events_event_composedpath"><code>event.composedPath()</code></a></li>
200<li><a href="#events_event_currenttarget"><code>event.currentTarget</code></a></li>
201<li><a href="#events_event_defaultprevented"><code>event.defaultPrevented</code></a></li>
202<li><a href="#events_event_eventphase"><code>event.eventPhase</code></a></li>
203<li><a href="#events_event_istrusted"><code>event.isTrusted</code></a></li>
204<li><a href="#events_event_preventdefault"><code>event.preventDefault()</code></a></li>
205<li><a href="#events_event_returnvalue"><code>event.returnValue</code></a></li>
206<li><a href="#events_event_srcelement"><code>event.srcElement</code></a></li>
207<li><a href="#events_event_stopimmediatepropagation"><code>event.stopImmediatePropagation()</code></a></li>
208<li><a href="#events_event_stoppropagation"><code>event.stopPropagation()</code></a></li>
209<li><a href="#events_event_target"><code>event.target</code></a></li>
210<li><a href="#events_event_timestamp"><code>event.timeStamp</code></a></li>
211<li><a href="#events_event_type"><code>event.type</code></a></li>
212</ul>
213</li>
214<li><a href="#events_class_eventtarget">Class: <code>EventTarget</code></a>
215<ul>
216<li><a href="#events_eventtarget_addeventlistener_type_listener_options"><code>eventTarget.addEventListener(type, listener[, options])</code></a></li>
217<li><a href="#events_eventtarget_dispatchevent_event"><code>eventTarget.dispatchEvent(event)</code></a></li>
218<li><a href="#events_eventtarget_removeeventlistener_type_listener"><code>eventTarget.removeEventListener(type, listener)</code></a></li>
219</ul>
220</li>
221<li><a href="#events_class_nodeeventtarget">Class: <code>NodeEventTarget</code></a>
222<ul>
223<li><a href="#events_nodeeventtarget_addlistener_type_listener_options"><code>nodeEventTarget.addListener(type, listener[, options])</code></a></li>
224<li><a href="#events_nodeeventtarget_eventnames"><code>nodeEventTarget.eventNames()</code></a></li>
225<li><a href="#events_nodeeventtarget_listenercount_type"><code>nodeEventTarget.listenerCount(type)</code></a></li>
226<li><a href="#events_nodeeventtarget_off_type_listener"><code>nodeEventTarget.off(type, listener)</code></a></li>
227<li><a href="#events_nodeeventtarget_on_type_listener_options"><code>nodeEventTarget.on(type, listener[, options])</code></a></li>
228<li><a href="#events_nodeeventtarget_once_type_listener_options"><code>nodeEventTarget.once(type, listener[, options])</code></a></li>
229<li><a href="#events_nodeeventtarget_removealllisteners_type"><code>nodeEventTarget.removeAllListeners([type])</code></a></li>
230<li><a href="#events_nodeeventtarget_removelistener_type_listener"><code>nodeEventTarget.removeListener(type, listener)</code></a></li>
231</ul>
232</li>
233</ul>
234</li>
235</ul>
236</li>
237</ul></details>
238
239      <div id="apicontent">
240        <h2>Events<span><a class="mark" href="#events_events" id="events_events">#</a></span></h2>
241
242<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
243
244<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.20.1/lib/events.js">lib/events.js</a></p>
245<p>Much of the Node.js core API is built around an idiomatic asynchronous
246event-driven architecture in which certain kinds of objects (called "emitters")
247emit named events that cause <code>Function</code> objects ("listeners") to be called.</p>
248<p>For instance: a <a href="net.html#net_class_net_server"><code>net.Server</code></a> object emits an event each time a peer
249connects to it; a <a href="fs.html#fs_class_fs_readstream"><code>fs.ReadStream</code></a> emits an event when the file is opened;
250a <a href="stream.html">stream</a> emits an event whenever data is available to be read.</p>
251<p>All objects that emit events are instances of the <code>EventEmitter</code> class. These
252objects expose an <code>eventEmitter.on()</code> function that allows one or more
253functions to be attached to named events emitted by the object. Typically,
254event names are camel-cased strings but any valid JavaScript property key
255can be used.</p>
256<p>When the <code>EventEmitter</code> object emits an event, all of the functions attached
257to that specific event are called <em>synchronously</em>. Any values returned by the
258called listeners are <em>ignored</em> and discarded.</p>
259<p>The following example shows a simple <code>EventEmitter</code> instance with a single
260listener. The <code>eventEmitter.on()</code> method is used to register listeners, while
261the <code>eventEmitter.emit()</code> method is used to trigger the event.</p>
262<pre><code class="language-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
263
264<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyEmitter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_ inherited__">EventEmitter</span> {}
265
266<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>();
267myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> {
268  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'an event occurred!'</span>);
269});
270myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);</code></pre>
271<section><h3>Passing arguments and <code>this</code> to listeners<span><a class="mark" href="#events_passing_arguments_and_this_to_listeners" id="events_passing_arguments_and_this_to_listeners">#</a></span></h3>
272<p>The <code>eventEmitter.emit()</code> method allows an arbitrary set of arguments to be
273passed to the listener functions. Keep in mind that when
274an ordinary listener function is called, the standard <code>this</code> keyword
275is intentionally set to reference the <code>EventEmitter</code> instance to which the
276listener is attached.</p>
277<pre><code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>();
278myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-keyword">function</span>(<span class="hljs-params">a, b</span>) {
279  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(a, b, <span class="hljs-variable language_">this</span>, <span class="hljs-variable language_">this</span> === myEmitter);
280  <span class="hljs-comment">// Prints:</span>
281  <span class="hljs-comment">//   a b MyEmitter {</span>
282  <span class="hljs-comment">//     domain: null,</span>
283  <span class="hljs-comment">//     _events: { event: [Function] },</span>
284  <span class="hljs-comment">//     _eventsCount: 1,</span>
285  <span class="hljs-comment">//     _maxListeners: undefined } true</span>
286});
287myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>);</code></pre>
288<p>It is possible to use ES6 Arrow Functions as listeners, however, when doing so,
289the <code>this</code> keyword will no longer reference the <code>EventEmitter</code> instance:</p>
290<pre><code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>();
291myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">(<span class="hljs-params">a, b</span>) =></span> {
292  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(a, b, <span class="hljs-variable language_">this</span>);
293  <span class="hljs-comment">// Prints: a b {}</span>
294});
295myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>);</code></pre>
296</section><section><h3>Asynchronous vs. synchronous<span><a class="mark" href="#events_asynchronous_vs_synchronous" id="events_asynchronous_vs_synchronous">#</a></span></h3>
297<p>The <code>EventEmitter</code> calls all listeners synchronously in the order in which
298they were registered. This ensures the proper sequencing of
299events and helps avoid race conditions and logic errors. When appropriate,
300listener functions can switch to an asynchronous mode of operation using
301the <code>setImmediate()</code> or <code>process.nextTick()</code> methods:</p>
302<pre><code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>();
303myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">(<span class="hljs-params">a, b</span>) =></span> {
304  <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> {
305    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'this happens asynchronously'</span>);
306  });
307});
308myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>);</code></pre>
309</section><section><h3>Handling events only once<span><a class="mark" href="#events_handling_events_only_once" id="events_handling_events_only_once">#</a></span></h3>
310<p>When a listener is registered using the <code>eventEmitter.on()</code> method, that
311listener is invoked <em>every time</em> the named event is emitted.</p>
312<pre><code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>();
313<span class="hljs-keyword">let</span> m = <span class="hljs-number">0</span>;
314myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> {
315  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(++m);
316});
317myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
318<span class="hljs-comment">// Prints: 1</span>
319myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
320<span class="hljs-comment">// Prints: 2</span></code></pre>
321<p>Using the <code>eventEmitter.once()</code> method, it is possible to register a listener
322that is called at most once for a particular event. Once the event is emitted,
323the listener is unregistered and <em>then</em> called.</p>
324<pre><code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>();
325<span class="hljs-keyword">let</span> m = <span class="hljs-number">0</span>;
326myEmitter.<span class="hljs-title function_">once</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> {
327  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(++m);
328});
329myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
330<span class="hljs-comment">// Prints: 1</span>
331myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
332<span class="hljs-comment">// Ignored</span></code></pre>
333</section><section><h3>Error events<span><a class="mark" href="#events_error_events" id="events_error_events">#</a></span></h3>
334<p>When an error occurs within an <code>EventEmitter</code> instance, the typical action is
335for an <code>'error'</code> event to be emitted. These are treated as special cases
336within Node.js.</p>
337<p>If an <code>EventEmitter</code> does <em>not</em> have at least one listener registered for the
338<code>'error'</code> event, and an <code>'error'</code> event is emitted, the error is thrown, a
339stack trace is printed, and the Node.js process exits.</p>
340<pre><code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>();
341myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'whoops!'</span>));
342<span class="hljs-comment">// Throws and crashes Node.js</span></code></pre>
343<p>To guard against crashing the Node.js process the <a href="domain.html"><code>domain</code></a> module can be
344used. (Note, however, that the <code>domain</code> module is deprecated.)</p>
345<p>As a best practice, listeners should always be added for the <code>'error'</code> events.</p>
346<pre><code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>();
347myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
348  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'whoops! there was an error'</span>);
349});
350myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'whoops!'</span>));
351<span class="hljs-comment">// Prints: whoops! there was an error</span></code></pre>
352<p>It is possible to monitor <code>'error'</code> events without consuming the emitted error
353by installing a listener using the symbol <code>events.errorMonitor</code>.</p>
354<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>, errorMonitor } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
355
356<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>();
357myEmitter.<span class="hljs-title function_">on</span>(errorMonitor, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
358  <span class="hljs-title class_">MyMonitoringTool</span>.<span class="hljs-title function_">log</span>(err);
359});
360myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'whoops!'</span>));
361<span class="hljs-comment">// Still throws and crashes Node.js</span></code></pre>
362</section><section><h3>Capture rejections of promises<span><a class="mark" href="#events_capture_rejections_of_promises" id="events_capture_rejections_of_promises">#</a></span></h3>
363<p></p><div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - captureRejections is experimental.</div><p></p>
364<p>Using <code>async</code> functions with event handlers is problematic, because it
365can lead to an unhandled rejection in case of a thrown exception:</p>
366<pre><code class="language-js"><span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>();
367ee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span class="hljs-keyword">async</span> (value) => {
368  <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'kaboom'</span>);
369});</code></pre>
370<p>The <code>captureRejections</code> option in the <code>EventEmitter</code> constructor or the global
371setting change this behavior, installing a <code>.then(undefined, handler)</code>
372handler on the <code>Promise</code>. This handler routes the exception
373asynchronously to the <a href="#events_emitter_symbol_for_nodejs_rejection_err_eventname_args"><code>Symbol.for('nodejs.rejection')</code></a> method
374if there is one, or to <a href="#events_error_events"><code>'error'</code></a> event handler if there is none.</p>
375<pre><code class="language-js"><span class="hljs-keyword">const</span> ee1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>({ <span class="hljs-attr">captureRejections</span>: <span class="hljs-literal">true</span> });
376ee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span class="hljs-keyword">async</span> (value) => {
377  <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'kaboom'</span>);
378});
379
380ee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-variable language_">console</span>.<span class="hljs-property">log</span>);
381
382<span class="hljs-keyword">const</span> ee2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>({ <span class="hljs-attr">captureRejections</span>: <span class="hljs-literal">true</span> });
383ee2.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span class="hljs-keyword">async</span> (value) => {
384  <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'kaboom'</span>);
385});
386
387ee2[<span class="hljs-title class_">Symbol</span>.<span class="hljs-title function_">for</span>(<span class="hljs-string">'nodejs.rejection'</span>)] = <span class="hljs-variable language_">console</span>.<span class="hljs-property">log</span>;</code></pre>
388<p>Setting <code>events.captureRejections = true</code> will change the default for all
389new instances of <code>EventEmitter</code>.</p>
390<pre><code class="language-js"><span class="hljs-keyword">const</span> events = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
391events.<span class="hljs-property">captureRejections</span> = <span class="hljs-literal">true</span>;
392<span class="hljs-keyword">const</span> ee1 = <span class="hljs-keyword">new</span> events.<span class="hljs-title class_">EventEmitter</span>();
393ee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span class="hljs-keyword">async</span> (value) => {
394  <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'kaboom'</span>);
395});
396
397ee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-variable language_">console</span>.<span class="hljs-property">log</span>);</code></pre>
398<p>The <code>'error'</code> events that are generated by the <code>captureRejections</code> behavior
399do not have a catch handler to avoid infinite error loops: the
400recommendation is to <strong>not use <code>async</code> functions as <code>'error'</code> event handlers</strong>.</p>
401</section><section><h3>Class: <code>EventEmitter</code><span><a class="mark" href="#events_class_eventemitter" id="events_class_eventemitter">#</a></span></h3>
402<div class="api_metadata">
403<details class="changelog"><summary>History</summary>
404<table>
405<tbody><tr><th>Version</th><th>Changes</th></tr>
406<tr><td>v13.4.0, v12.16.0</td>
407<td><p>Added captureRejections option.</p></td></tr>
408<tr><td>v0.1.26</td>
409<td><p><span>Added in: v0.1.26</span></p></td></tr>
410</tbody></table>
411</details>
412</div>
413<p>The <code>EventEmitter</code> class is defined and exposed by the <code>events</code> module:</p>
414<pre><code class="language-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);</code></pre>
415<p>All <code>EventEmitter</code>s emit the event <code>'newListener'</code> when new listeners are
416added and <code>'removeListener'</code> when existing listeners are removed.</p>
417<p>It supports the following option:</p>
418<ul>
419<li><code>captureRejections</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> It enables
420<a href="#events_capture_rejections_of_promises">automatic capturing of promise rejection</a>.
421<strong>Default:</strong> <code>false</code>.</li>
422</ul>
423<h4>Event: <code>'newListener'</code><span><a class="mark" href="#events_event_newlistener" id="events_event_newlistener">#</a></span></h4>
424<div class="api_metadata">
425<span>Added in: v0.1.26</span>
426</div>
427<ul>
428<li><code>eventName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a> The name of the event being listened for</li>
429<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The event handler function</li>
430</ul>
431<p>The <code>EventEmitter</code> instance will emit its own <code>'newListener'</code> event <em>before</em>
432a listener is added to its internal array of listeners.</p>
433<p>Listeners registered for the <code>'newListener'</code> event are passed the event
434name and a reference to the listener being added.</p>
435<p>The fact that the event is triggered before adding the listener has a subtle
436but important side effect: any <em>additional</em> listeners registered to the same
437<code>name</code> <em>within</em> the <code>'newListener'</code> callback are inserted <em>before</em> the
438listener that is in the process of being added.</p>
439<pre><code class="language-js"><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyEmitter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_ inherited__">EventEmitter</span> {}
440
441<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>();
442<span class="hljs-comment">// Only do this once so we don't loop forever</span>
443myEmitter.<span class="hljs-title function_">once</span>(<span class="hljs-string">'newListener'</span>, <span class="hljs-function">(<span class="hljs-params">event, listener</span>) =></span> {
444  <span class="hljs-keyword">if</span> (event === <span class="hljs-string">'event'</span>) {
445    <span class="hljs-comment">// Insert a new listener in front</span>
446    myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> {
447      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'B'</span>);
448    });
449  }
450});
451myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> {
452  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'A'</span>);
453});
454myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
455<span class="hljs-comment">// Prints:</span>
456<span class="hljs-comment">//   B</span>
457<span class="hljs-comment">//   A</span></code></pre>
458<h4>Event: <code>'removeListener'</code><span><a class="mark" href="#events_event_removelistener" id="events_event_removelistener">#</a></span></h4>
459<div class="api_metadata">
460<details class="changelog"><summary>History</summary>
461<table>
462<tbody><tr><th>Version</th><th>Changes</th></tr>
463<tr><td>v6.1.0, v4.7.0</td>
464<td><p>For listeners attached using <code>.once()</code>, the <code>listener</code> argument now yields the original listener function.</p></td></tr>
465<tr><td>v0.9.3</td>
466<td><p><span>Added in: v0.9.3</span></p></td></tr>
467</tbody></table>
468</details>
469</div>
470<ul>
471<li><code>eventName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a> The event name</li>
472<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The event handler function</li>
473</ul>
474<p>The <code>'removeListener'</code> event is emitted <em>after</em> the <code>listener</code> is removed.</p>
475<h4><code>emitter.addListener(eventName, listener)</code><span><a class="mark" href="#events_emitter_addlistener_eventname_listener" id="events_emitter_addlistener_eventname_listener">#</a></span></h4>
476<div class="api_metadata">
477<span>Added in: v0.1.26</span>
478</div>
479<ul>
480<li><code>eventName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a></li>
481<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
482</ul>
483<p>Alias for <code>emitter.on(eventName, listener)</code>.</p>
484<h4><code>emitter.emit(eventName[, ...args])</code><span><a class="mark" href="#events_emitter_emit_eventname_args" id="events_emitter_emit_eventname_args">#</a></span></h4>
485<div class="api_metadata">
486<span>Added in: v0.1.26</span>
487</div>
488<ul>
489<li><code>eventName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a></li>
490<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
491<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
492</ul>
493<p>Synchronously calls each of the listeners registered for the event named
494<code>eventName</code>, in the order they were registered, passing the supplied arguments
495to each.</p>
496<p>Returns <code>true</code> if the event had listeners, <code>false</code> otherwise.</p>
497<pre><code class="language-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
498<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>();
499
500<span class="hljs-comment">// First listener</span>
501myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-keyword">function</span> <span class="hljs-title function_">firstListener</span>(<span class="hljs-params"></span>) {
502  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Helloooo! first listener'</span>);
503});
504<span class="hljs-comment">// Second listener</span>
505myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-keyword">function</span> <span class="hljs-title function_">secondListener</span>(<span class="hljs-params">arg1, arg2</span>) {
506  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`event with parameters <span class="hljs-subst">${arg1}</span>, <span class="hljs-subst">${arg2}</span> in second listener`</span>);
507});
508<span class="hljs-comment">// Third listener</span>
509myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-keyword">function</span> <span class="hljs-title function_">thirdListener</span>(<span class="hljs-params">...args</span>) {
510  <span class="hljs-keyword">const</span> parameters = args.<span class="hljs-title function_">join</span>(<span class="hljs-string">', '</span>);
511  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`event with parameters <span class="hljs-subst">${parameters}</span> in third listener`</span>);
512});
513
514<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myEmitter.<span class="hljs-title function_">listeners</span>(<span class="hljs-string">'event'</span>));
515
516myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>);
517
518<span class="hljs-comment">// Prints:</span>
519<span class="hljs-comment">// [</span>
520<span class="hljs-comment">//   [Function: firstListener],</span>
521<span class="hljs-comment">//   [Function: secondListener],</span>
522<span class="hljs-comment">//   [Function: thirdListener]</span>
523<span class="hljs-comment">// ]</span>
524<span class="hljs-comment">// Helloooo! first listener</span>
525<span class="hljs-comment">// event with parameters 1, 2 in second listener</span>
526<span class="hljs-comment">// event with parameters 1, 2, 3, 4, 5 in third listener</span></code></pre>
527<h4><code>emitter.eventNames()</code><span><a class="mark" href="#events_emitter_eventnames" id="events_emitter_eventnames">#</a></span></h4>
528<div class="api_metadata">
529<span>Added in: v6.0.0</span>
530</div>
531<ul>
532<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&#x3C;Array></a></li>
533</ul>
534<p>Returns an array listing the events for which the emitter has registered
535listeners. The values in the array are strings or <code>Symbol</code>s.</p>
536<pre><code class="language-js"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
537<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>();
538myEE.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">() =></span> {});
539myEE.<span class="hljs-title function_">on</span>(<span class="hljs-string">'bar'</span>, <span class="hljs-function">() =></span> {});
540
541<span class="hljs-keyword">const</span> sym = <span class="hljs-title class_">Symbol</span>(<span class="hljs-string">'symbol'</span>);
542myEE.<span class="hljs-title function_">on</span>(sym, <span class="hljs-function">() =></span> {});
543
544<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myEE.<span class="hljs-title function_">eventNames</span>());
545<span class="hljs-comment">// Prints: [ 'foo', 'bar', Symbol(symbol) ]</span></code></pre>
546<h4><code>emitter.getMaxListeners()</code><span><a class="mark" href="#events_emitter_getmaxlisteners" id="events_emitter_getmaxlisteners">#</a></span></h4>
547<div class="api_metadata">
548<span>Added in: v1.0.0</span>
549</div>
550<ul>
551<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
552</ul>
553<p>Returns the current max listener value for the <code>EventEmitter</code> which is either
554set by <a href="#events_emitter_setmaxlisteners_n"><code>emitter.setMaxListeners(n)</code></a> or defaults to
555<a href="#events_events_defaultmaxlisteners"><code>events.defaultMaxListeners</code></a>.</p>
556<h4><code>emitter.listenerCount(eventName)</code><span><a class="mark" href="#events_emitter_listenercount_eventname" id="events_emitter_listenercount_eventname">#</a></span></h4>
557<div class="api_metadata">
558<span>Added in: v3.2.0</span>
559</div>
560<ul>
561<li><code>eventName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a> The name of the event being listened for</li>
562<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
563</ul>
564<p>Returns the number of listeners listening to the event named <code>eventName</code>.</p>
565<h4><code>emitter.listeners(eventName)</code><span><a class="mark" href="#events_emitter_listeners_eventname" id="events_emitter_listeners_eventname">#</a></span></h4>
566<div class="api_metadata">
567<details class="changelog"><summary>History</summary>
568<table>
569<tbody><tr><th>Version</th><th>Changes</th></tr>
570<tr><td>v7.0.0</td>
571<td><p>For listeners attached using <code>.once()</code> this returns the original listeners instead of wrapper functions now.</p></td></tr>
572<tr><td>v0.1.26</td>
573<td><p><span>Added in: v0.1.26</span></p></td></tr>
574</tbody></table>
575</details>
576</div>
577<ul>
578<li><code>eventName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a></li>
579<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function[]></a></li>
580</ul>
581<p>Returns a copy of the array of listeners for the event named <code>eventName</code>.</p>
582<pre><code class="language-js">server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
583  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'someone connected!'</span>);
584});
585<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(util.<span class="hljs-title function_">inspect</span>(server.<span class="hljs-title function_">listeners</span>(<span class="hljs-string">'connection'</span>)));
586<span class="hljs-comment">// Prints: [ [Function] ]</span></code></pre>
587<h4><code>emitter.off(eventName, listener)</code><span><a class="mark" href="#events_emitter_off_eventname_listener" id="events_emitter_off_eventname_listener">#</a></span></h4>
588<div class="api_metadata">
589<span>Added in: v10.0.0</span>
590</div>
591<ul>
592<li><code>eventName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a></li>
593<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
594<li>Returns: <a href="events.html#events_class_eventemitter" class="type">&#x3C;EventEmitter></a></li>
595</ul>
596<p>Alias for <a href="#events_emitter_removelistener_eventname_listener"><code>emitter.removeListener()</code></a>.</p>
597<h4><code>emitter.on(eventName, listener)</code><span><a class="mark" href="#events_emitter_on_eventname_listener" id="events_emitter_on_eventname_listener">#</a></span></h4>
598<div class="api_metadata">
599<span>Added in: v0.1.101</span>
600</div>
601<ul>
602<li><code>eventName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a> The name of the event.</li>
603<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The callback function</li>
604<li>Returns: <a href="events.html#events_class_eventemitter" class="type">&#x3C;EventEmitter></a></li>
605</ul>
606<p>Adds the <code>listener</code> function to the end of the listeners array for the
607event named <code>eventName</code>. No checks are made to see if the <code>listener</code> has
608already been added. Multiple calls passing the same combination of <code>eventName</code>
609and <code>listener</code> will result in the <code>listener</code> being added, and called, multiple
610times.</p>
611<pre><code class="language-js">server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
612  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'someone connected!'</span>);
613});</code></pre>
614<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p>
615<p>By default, event listeners are invoked in the order they are added. The
616<code>emitter.prependListener()</code> method can be used as an alternative to add the
617event listener to the beginning of the listeners array.</p>
618<pre><code class="language-js"><span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>();
619myEE.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">() =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'a'</span>));
620myEE.<span class="hljs-title function_">prependListener</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">() =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'b'</span>));
621myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>);
622<span class="hljs-comment">// Prints:</span>
623<span class="hljs-comment">//   b</span>
624<span class="hljs-comment">//   a</span></code></pre>
625<h4><code>emitter.once(eventName, listener)</code><span><a class="mark" href="#events_emitter_once_eventname_listener" id="events_emitter_once_eventname_listener">#</a></span></h4>
626<div class="api_metadata">
627<span>Added in: v0.3.0</span>
628</div>
629<ul>
630<li><code>eventName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a> The name of the event.</li>
631<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The callback function</li>
632<li>Returns: <a href="events.html#events_class_eventemitter" class="type">&#x3C;EventEmitter></a></li>
633</ul>
634<p>Adds a <strong>one-time</strong> <code>listener</code> function for the event named <code>eventName</code>. The
635next time <code>eventName</code> is triggered, this listener is removed and then invoked.</p>
636<pre><code class="language-js">server.<span class="hljs-title function_">once</span>(<span class="hljs-string">'connection'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
637  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Ah, we have our first user!'</span>);
638});</code></pre>
639<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p>
640<p>By default, event listeners are invoked in the order they are added. The
641<code>emitter.prependOnceListener()</code> method can be used as an alternative to add the
642event listener to the beginning of the listeners array.</p>
643<pre><code class="language-js"><span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>();
644myEE.<span class="hljs-title function_">once</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">() =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'a'</span>));
645myEE.<span class="hljs-title function_">prependOnceListener</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">() =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'b'</span>));
646myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>);
647<span class="hljs-comment">// Prints:</span>
648<span class="hljs-comment">//   b</span>
649<span class="hljs-comment">//   a</span></code></pre>
650<h4><code>emitter.prependListener(eventName, listener)</code><span><a class="mark" href="#events_emitter_prependlistener_eventname_listener" id="events_emitter_prependlistener_eventname_listener">#</a></span></h4>
651<div class="api_metadata">
652<span>Added in: v6.0.0</span>
653</div>
654<ul>
655<li><code>eventName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a> The name of the event.</li>
656<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The callback function</li>
657<li>Returns: <a href="events.html#events_class_eventemitter" class="type">&#x3C;EventEmitter></a></li>
658</ul>
659<p>Adds the <code>listener</code> function to the <em>beginning</em> of the listeners array for the
660event named <code>eventName</code>. No checks are made to see if the <code>listener</code> has
661already been added. Multiple calls passing the same combination of <code>eventName</code>
662and <code>listener</code> will result in the <code>listener</code> being added, and called, multiple
663times.</p>
664<pre><code class="language-js">server.<span class="hljs-title function_">prependListener</span>(<span class="hljs-string">'connection'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
665  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'someone connected!'</span>);
666});</code></pre>
667<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p>
668<h4><code>emitter.prependOnceListener(eventName, listener)</code><span><a class="mark" href="#events_emitter_prependoncelistener_eventname_listener" id="events_emitter_prependoncelistener_eventname_listener">#</a></span></h4>
669<div class="api_metadata">
670<span>Added in: v6.0.0</span>
671</div>
672<ul>
673<li><code>eventName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a> The name of the event.</li>
674<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The callback function</li>
675<li>Returns: <a href="events.html#events_class_eventemitter" class="type">&#x3C;EventEmitter></a></li>
676</ul>
677<p>Adds a <strong>one-time</strong> <code>listener</code> function for the event named <code>eventName</code> to the
678<em>beginning</em> of the listeners array. The next time <code>eventName</code> is triggered, this
679listener is removed, and then invoked.</p>
680<pre><code class="language-js">server.<span class="hljs-title function_">prependOnceListener</span>(<span class="hljs-string">'connection'</span>, <span class="hljs-function">(<span class="hljs-params">stream</span>) =></span> {
681  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Ah, we have our first user!'</span>);
682});</code></pre>
683<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p>
684<h4><code>emitter.removeAllListeners([eventName])</code><span><a class="mark" href="#events_emitter_removealllisteners_eventname" id="events_emitter_removealllisteners_eventname">#</a></span></h4>
685<div class="api_metadata">
686<span>Added in: v0.1.26</span>
687</div>
688<ul>
689<li><code>eventName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a></li>
690<li>Returns: <a href="events.html#events_class_eventemitter" class="type">&#x3C;EventEmitter></a></li>
691</ul>
692<p>Removes all listeners, or those of the specified <code>eventName</code>.</p>
693<p>It is bad practice to remove listeners added elsewhere in the code,
694particularly when the <code>EventEmitter</code> instance was created by some other
695component or module (e.g. sockets or file streams).</p>
696<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p>
697<h4><code>emitter.removeListener(eventName, listener)</code><span><a class="mark" href="#events_emitter_removelistener_eventname_listener" id="events_emitter_removelistener_eventname_listener">#</a></span></h4>
698<div class="api_metadata">
699<span>Added in: v0.1.26</span>
700</div>
701<ul>
702<li><code>eventName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a></li>
703<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
704<li>Returns: <a href="events.html#events_class_eventemitter" class="type">&#x3C;EventEmitter></a></li>
705</ul>
706<p>Removes the specified <code>listener</code> from the listener array for the event named
707<code>eventName</code>.</p>
708<pre><code class="language-js"><span class="hljs-keyword">const</span> <span class="hljs-title function_">callback</span> = (<span class="hljs-params">stream</span>) => {
709  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'someone connected!'</span>);
710};
711server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, callback);
712<span class="hljs-comment">// ...</span>
713server.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'connection'</span>, callback);</code></pre>
714<p><code>removeListener()</code> will remove, at most, one instance of a listener from the
715listener array. If any single listener has been added multiple times to the
716listener array for the specified <code>eventName</code>, then <code>removeListener()</code> must be
717called multiple times to remove each instance.</p>
718<p>Once an event is emitted, all listeners attached to it at the
719time of emitting are called in order. This implies that any
720<code>removeListener()</code> or <code>removeAllListeners()</code> calls <em>after</em> emitting and
721<em>before</em> the last listener finishes execution will not remove them from
722<code>emit()</code> in progress. Subsequent events behave as expected.</p>
723<pre><code class="language-js"><span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>();
724
725<span class="hljs-keyword">const</span> <span class="hljs-title function_">callbackA</span> = (<span class="hljs-params"></span>) => {
726  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'A'</span>);
727  myEmitter.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'event'</span>, callbackB);
728};
729
730<span class="hljs-keyword">const</span> <span class="hljs-title function_">callbackB</span> = (<span class="hljs-params"></span>) => {
731  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'B'</span>);
732};
733
734myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, callbackA);
735
736myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, callbackB);
737
738<span class="hljs-comment">// callbackA removes listener callbackB but it will still be called.</span>
739<span class="hljs-comment">// Internal listener array at time of emit [callbackA, callbackB]</span>
740myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
741<span class="hljs-comment">// Prints:</span>
742<span class="hljs-comment">//   A</span>
743<span class="hljs-comment">//   B</span>
744
745<span class="hljs-comment">// callbackB is now removed.</span>
746<span class="hljs-comment">// Internal listener array [callbackA]</span>
747myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);
748<span class="hljs-comment">// Prints:</span>
749<span class="hljs-comment">//   A</span></code></pre>
750<p>Because listeners are managed using an internal array, calling this will
751change the position indices of any listener registered <em>after</em> the listener
752being removed. This will not impact the order in which listeners are called,
753but it means that any copies of the listener array as returned by
754the <code>emitter.listeners()</code> method will need to be recreated.</p>
755<p>When a single function has been added as a handler multiple times for a single
756event (as in the example below), <code>removeListener()</code> will remove the most
757recently added instance. In the example the <code>once('ping')</code>
758listener is removed:</p>
759<pre><code class="language-js"><span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>();
760
761<span class="hljs-keyword">function</span> <span class="hljs-title function_">pong</span>(<span class="hljs-params"></span>) {
762  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'pong'</span>);
763}
764
765ee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'ping'</span>, pong);
766ee.<span class="hljs-title function_">once</span>(<span class="hljs-string">'ping'</span>, pong);
767ee.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'ping'</span>, pong);
768
769ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'ping'</span>);
770ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'ping'</span>);</code></pre>
771<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p>
772<h4><code>emitter.setMaxListeners(n)</code><span><a class="mark" href="#events_emitter_setmaxlisteners_n" id="events_emitter_setmaxlisteners_n">#</a></span></h4>
773<div class="api_metadata">
774<span>Added in: v0.3.5</span>
775</div>
776<ul>
777<li><code>n</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
778<li>Returns: <a href="events.html#events_class_eventemitter" class="type">&#x3C;EventEmitter></a></li>
779</ul>
780<p>By default <code>EventEmitter</code>s will print a warning if more than <code>10</code> listeners are
781added for a particular event. This is a useful default that helps finding
782memory leaks. The <code>emitter.setMaxListeners()</code> method allows the limit to be
783modified for this specific <code>EventEmitter</code> instance. The value can be set to
784<code>Infinity</code> (or <code>0</code>) to indicate an unlimited number of listeners.</p>
785<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p>
786<h4><code>emitter.rawListeners(eventName)</code><span><a class="mark" href="#events_emitter_rawlisteners_eventname" id="events_emitter_rawlisteners_eventname">#</a></span></h4>
787<div class="api_metadata">
788<span>Added in: v9.4.0</span>
789</div>
790<ul>
791<li><code>eventName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a></li>
792<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function[]></a></li>
793</ul>
794<p>Returns a copy of the array of listeners for the event named <code>eventName</code>,
795including any wrappers (such as those created by <code>.once()</code>).</p>
796<pre><code class="language-js"><span class="hljs-keyword">const</span> emitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>();
797emitter.<span class="hljs-title function_">once</span>(<span class="hljs-string">'log'</span>, <span class="hljs-function">() =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'log once'</span>));
798
799<span class="hljs-comment">// Returns a new Array with a function `onceWrapper` which has a property</span>
800<span class="hljs-comment">// `listener` which contains the original listener bound above</span>
801<span class="hljs-keyword">const</span> listeners = emitter.<span class="hljs-title function_">rawListeners</span>(<span class="hljs-string">'log'</span>);
802<span class="hljs-keyword">const</span> logFnWrapper = listeners[<span class="hljs-number">0</span>];
803
804<span class="hljs-comment">// Logs "log once" to the console and does not unbind the `once` event</span>
805logFnWrapper.<span class="hljs-title function_">listener</span>();
806
807<span class="hljs-comment">// Logs "log once" to the console and removes the listener</span>
808<span class="hljs-title function_">logFnWrapper</span>();
809
810emitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'log'</span>, <span class="hljs-function">() =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'log persistently'</span>));
811<span class="hljs-comment">// Will return a new Array with a single function bound by `.on()` above</span>
812<span class="hljs-keyword">const</span> newListeners = emitter.<span class="hljs-title function_">rawListeners</span>(<span class="hljs-string">'log'</span>);
813
814<span class="hljs-comment">// Logs "log persistently" twice</span>
815newListeners[<span class="hljs-number">0</span>]();
816emitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'log'</span>);</code></pre>
817<h4><code>emitter[Symbol.for('nodejs.rejection')](err, eventName[, ...args])</code><span><a class="mark" href="#events_emitter_symbol_for_nodejs_rejection_err_eventname_args" id="events_emitter_symbol_for_nodejs_rejection_err_eventname_args">#</a></span></h4>
818<div class="api_metadata">
819<span>Added in: v13.4.0, v12.16.0</span>
820</div>
821<p></p><div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - captureRejections is experimental.</div><p></p>
822<ul>
823<li><code>err</code> Error</li>
824<li><code>eventName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a></li>
825<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a></li>
826</ul>
827<p>The <code>Symbol.for('nodejs.rejection')</code> method is called in case a
828promise rejection happens when emitting an event and
829<a href="#events_capture_rejections_of_promises"><code>captureRejections</code></a> is enabled on the emitter.
830It is possible to use <a href="#events_events_capturerejectionsymbol"><code>events.captureRejectionSymbol</code></a> in
831place of <code>Symbol.for('nodejs.rejection')</code>.</p>
832<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>, captureRejectionSymbol } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
833
834<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyClass</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_ inherited__">EventEmitter</span> {
835  <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) {
836    <span class="hljs-variable language_">super</span>({ <span class="hljs-attr">captureRejections</span>: <span class="hljs-literal">true</span> });
837  }
838
839  [captureRejectionSymbol](err, event, ...args) {
840    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'rejection happened for'</span>, event, <span class="hljs-string">'with'</span>, err, ...args);
841    <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">destroy</span>(err);
842  }
843
844  <span class="hljs-title function_">destroy</span>(<span class="hljs-params">err</span>) {
845    <span class="hljs-comment">// Tear the resource down here.</span>
846  }
847}</code></pre>
848</section><section><h3><code>events.defaultMaxListeners</code><span><a class="mark" href="#events_events_defaultmaxlisteners" id="events_events_defaultmaxlisteners">#</a></span></h3>
849<div class="api_metadata">
850<span>Added in: v0.11.2</span>
851</div>
852<p>By default, a maximum of <code>10</code> listeners can be registered for any single
853event. This limit can be changed for individual <code>EventEmitter</code> instances
854using the <a href="#events_emitter_setmaxlisteners_n"><code>emitter.setMaxListeners(n)</code></a> method. To change the default
855for <em>all</em> <code>EventEmitter</code> instances, the <code>events.defaultMaxListeners</code>
856property can be used. If this value is not a positive number, a <code>RangeError</code>
857is thrown.</p>
858<p>Take caution when setting the <code>events.defaultMaxListeners</code> because the
859change affects <em>all</em> <code>EventEmitter</code> instances, including those created before
860the change is made. However, calling <a href="#events_emitter_setmaxlisteners_n"><code>emitter.setMaxListeners(n)</code></a> still has
861precedence over <code>events.defaultMaxListeners</code>.</p>
862<p>This is not a hard limit. The <code>EventEmitter</code> instance will allow
863more listeners to be added but will output a trace warning to stderr indicating
864that a "possible EventEmitter memory leak" has been detected. For any single
865<code>EventEmitter</code>, the <code>emitter.getMaxListeners()</code> and <code>emitter.setMaxListeners()</code>
866methods can be used to temporarily avoid this warning:</p>
867<pre><code class="language-js">emitter.<span class="hljs-title function_">setMaxListeners</span>(emitter.<span class="hljs-title function_">getMaxListeners</span>() + <span class="hljs-number">1</span>);
868emitter.<span class="hljs-title function_">once</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> {
869  <span class="hljs-comment">// do stuff</span>
870  emitter.<span class="hljs-title function_">setMaxListeners</span>(<span class="hljs-title class_">Math</span>.<span class="hljs-title function_">max</span>(emitter.<span class="hljs-title function_">getMaxListeners</span>() - <span class="hljs-number">1</span>, <span class="hljs-number">0</span>));
871});</code></pre>
872<p>The <a href="cli.html#cli_trace_warnings"><code>--trace-warnings</code></a> command-line flag can be used to display the
873stack trace for such warnings.</p>
874<p>The emitted warning can be inspected with <a href="process.html#process_event_warning"><code>process.on('warning')</code></a> and will
875have the additional <code>emitter</code>, <code>type</code> and <code>count</code> properties, referring to
876the event emitter instance, the event’s name and the number of attached
877listeners, respectively.
878Its <code>name</code> property is set to <code>'MaxListenersExceededWarning'</code>.</p>
879</section><section><h3><code>events.errorMonitor</code><span><a class="mark" href="#events_events_errormonitor" id="events_events_errormonitor">#</a></span></h3>
880<div class="api_metadata">
881<span>Added in: v13.6.0, v12.17.0</span>
882</div>
883<p>This symbol shall be used to install a listener for only monitoring <code>'error'</code>
884events. Listeners installed using this symbol are called before the regular
885<code>'error'</code> listeners are called.</p>
886<p>Installing a listener using this symbol does not change the behavior once an
887<code>'error'</code> event is emitted, therefore the process will still crash if no
888regular <code>'error'</code> listener is installed.</p>
889</section><section><h3><code>events.getEventListeners(emitterOrTarget, eventName)</code><span><a class="mark" href="#events_events_geteventlisteners_emitterortarget_eventname" id="events_events_geteventlisteners_emitterortarget_eventname">#</a></span></h3>
890<div class="api_metadata">
891<span>Added in: v14.17.0</span>
892</div>
893<ul>
894<li><code>emitterOrTarget</code> <a href="events.html#events_class_eventemitter" class="type">&#x3C;EventEmitter></a> | <a href="events.html#events_class_eventtarget" class="type">&#x3C;EventTarget></a></li>
895<li><code>eventName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a></li>
896<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function[]></a></li>
897</ul>
898<p>Returns a copy of the array of listeners for the event named <code>eventName</code>.</p>
899<p>For <code>EventEmitter</code>s this behaves exactly the same as calling <code>.listeners</code> on
900the emitter.</p>
901<p>For <code>EventTarget</code>s this is the only way to get the event listeners for the
902event target. This is useful for debugging and diagnostic purposes.</p>
903<pre><code class="language-js"><span class="hljs-keyword">const</span> { getEventListeners, <span class="hljs-title class_">EventEmitter</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
904
905{
906  <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>();
907  <span class="hljs-keyword">const</span> <span class="hljs-title function_">listener</span> = (<span class="hljs-params"></span>) => <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Events are fun'</span>);
908  ee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, listener);
909  <span class="hljs-title function_">getEventListeners</span>(ee, <span class="hljs-string">'foo'</span>); <span class="hljs-comment">// [listener]</span>
910}
911{
912  <span class="hljs-keyword">const</span> et = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventTarget</span>();
913  <span class="hljs-keyword">const</span> <span class="hljs-title function_">listener</span> = (<span class="hljs-params"></span>) => <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Events are fun'</span>);
914  et.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, listener);
915  <span class="hljs-title function_">getEventListeners</span>(et, <span class="hljs-string">'foo'</span>); <span class="hljs-comment">// [listener]</span>
916}</code></pre>
917</section><section><h3><code>events.once(emitter, name[, options])</code><span><a class="mark" href="#events_events_once_emitter_name_options" id="events_events_once_emitter_name_options">#</a></span></h3>
918<div class="api_metadata">
919<details class="changelog"><summary>History</summary>
920<table>
921<tbody><tr><th>Version</th><th>Changes</th></tr>
922<tr><td>v14.17.0</td>
923<td><p>The <code>signal</code> option is supported now.</p></td></tr>
924<tr><td>v11.13.0, v10.16.0</td>
925<td><p><span>Added in: v11.13.0, v10.16.0</span></p></td></tr>
926</tbody></table>
927</details>
928</div>
929<ul>
930<li><code>emitter</code> <a href="events.html#events_class_eventemitter" class="type">&#x3C;EventEmitter></a></li>
931<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
932<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
933<ul>
934<li><code>signal</code> <a href="globals.html#globals_class_abortsignal" class="type">&#x3C;AbortSignal></a> Can be used to cancel waiting for the event.</li>
935</ul>
936</li>
937<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
938</ul>
939<p>Creates a <code>Promise</code> that is fulfilled when the <code>EventEmitter</code> emits the given
940event or that is rejected if the <code>EventEmitter</code> emits <code>'error'</code> while waiting.
941The <code>Promise</code> will resolve with an array of all the arguments emitted to the
942given event.</p>
943<p>This method is intentionally generic and works with the web platform
944<a href="https://dom.spec.whatwg.org/#interface-eventtarget">EventTarget</a> interface, which has no special
945<code>'error'</code> event semantics and does not listen to the <code>'error'</code> event.</p>
946<pre><code class="language-js"><span class="hljs-keyword">const</span> { once, <span class="hljs-title class_">EventEmitter</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
947
948<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">run</span>(<span class="hljs-params"></span>) {
949  <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>();
950
951  process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> {
952    ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'myevent'</span>, <span class="hljs-number">42</span>);
953  });
954
955  <span class="hljs-keyword">const</span> [value] = <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(ee, <span class="hljs-string">'myevent'</span>);
956  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value);
957
958  <span class="hljs-keyword">const</span> err = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'kaboom'</span>);
959  process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> {
960    ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'error'</span>, err);
961  });
962
963  <span class="hljs-keyword">try</span> {
964    <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(ee, <span class="hljs-string">'myevent'</span>);
965  } <span class="hljs-keyword">catch</span> (err) {
966    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'error happened'</span>, err);
967  }
968}
969
970<span class="hljs-title function_">run</span>();</code></pre>
971<p>The special handling of the <code>'error'</code> event is only used when <code>events.once()</code>
972is used to wait for another event. If <code>events.once()</code> is used to wait for the
973'<code>error'</code> event itself, then it is treated as any other kind of event without
974special handling:</p>
975<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>, once } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
976
977<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>();
978
979<span class="hljs-title function_">once</span>(ee, <span class="hljs-string">'error'</span>)
980  .<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">[err]</span>) =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'ok'</span>, err.<span class="hljs-property">message</span>))
981  .<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-params">err</span>) =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'error'</span>, err.<span class="hljs-property">message</span>));
982
983ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'error'</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'boom'</span>));
984
985<span class="hljs-comment">// Prints: ok boom</span></code></pre>
986<p>An <a href="globals.html#globals_class_abortsignal" class="type">&#x3C;AbortSignal></a> can be used to cancel waiting for the event:</p>
987<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>, once } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
988
989<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>();
990<span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>();
991
992<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params">emitter, event, signal</span>) {
993  <span class="hljs-keyword">try</span> {
994    <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(emitter, event, { signal });
995    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'event emitted!'</span>);
996  } <span class="hljs-keyword">catch</span> (error) {
997    <span class="hljs-keyword">if</span> (error.<span class="hljs-property">name</span> === <span class="hljs-string">'AbortError'</span>) {
998      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'Waiting for the event was canceled!'</span>);
999    } <span class="hljs-keyword">else</span> {
1000      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'There was an error'</span>, error.<span class="hljs-property">message</span>);
1001    }
1002  }
1003}
1004
1005<span class="hljs-title function_">foo</span>(ee, <span class="hljs-string">'foo'</span>, ac.<span class="hljs-property">signal</span>);
1006ac.<span class="hljs-title function_">abort</span>(); <span class="hljs-comment">// Abort waiting for the event</span>
1007ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); <span class="hljs-comment">// Prints: Waiting for the event was canceled!</span></code></pre>
1008<h4>Awaiting multiple events emitted on <code>process.nextTick()</code><span><a class="mark" href="#events_awaiting_multiple_events_emitted_on_process_nexttick" id="events_awaiting_multiple_events_emitted_on_process_nexttick">#</a></span></h4>
1009<p>There is an edge case worth noting when using the <code>events.once()</code> function
1010to await multiple events emitted on in the same batch of <code>process.nextTick()</code>
1011operations, or whenever multiple events are emitted synchronously. Specifically,
1012because the <code>process.nextTick()</code> queue is drained before the <code>Promise</code> microtask
1013queue, and because <code>EventEmitter</code> emits all events synchronously, it is possible
1014for <code>events.once()</code> to miss an event.</p>
1015<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>, once } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
1016
1017<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>();
1018
1019<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) {
1020  <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(myEE, <span class="hljs-string">'bar'</span>);
1021  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'bar'</span>);
1022
1023  <span class="hljs-comment">// This Promise will never resolve because the 'foo' event will</span>
1024  <span class="hljs-comment">// have already been emitted before the Promise is created.</span>
1025  <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(myEE, <span class="hljs-string">'foo'</span>);
1026  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'foo'</span>);
1027}
1028
1029process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> {
1030  myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'bar'</span>);
1031  myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>);
1032});
1033
1034<span class="hljs-title function_">foo</span>().<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'done'</span>));</code></pre>
1035<p>To catch both events, create each of the Promises <em>before</em> awaiting either
1036of them, then it becomes possible to use <code>Promise.all()</code>, <code>Promise.race()</code>,
1037or <code>Promise.allSettled()</code>:</p>
1038<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>, once } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
1039
1040<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>();
1041
1042<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) {
1043  <span class="hljs-keyword">await</span> <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">all</span>([<span class="hljs-title function_">once</span>(myEE, <span class="hljs-string">'bar'</span>), <span class="hljs-title function_">once</span>(myEE, <span class="hljs-string">'foo'</span>)]);
1044  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'bar'</span>);
1045}
1046
1047process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> {
1048  myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'bar'</span>);
1049  myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>);
1050});
1051
1052<span class="hljs-title function_">foo</span>().<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'done'</span>));</code></pre>
1053</section><section><h3><code>events.captureRejections</code><span><a class="mark" href="#events_events_capturerejections" id="events_events_capturerejections">#</a></span></h3>
1054<div class="api_metadata">
1055<span>Added in: v13.4.0, v12.16.0</span>
1056</div>
1057<p></p><div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - captureRejections is experimental.</div><p></p>
1058<p>Value: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></p>
1059<p>Change the default <code>captureRejections</code> option on all new <code>EventEmitter</code> objects.</p>
1060</section><section><h3><code>events.captureRejectionSymbol</code><span><a class="mark" href="#events_events_capturerejectionsymbol" id="events_events_capturerejectionsymbol">#</a></span></h3>
1061<div class="api_metadata">
1062<span>Added in: v13.4.0, v12.16.0</span>
1063</div>
1064<p></p><div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - captureRejections is experimental.</div><p></p>
1065<p>Value: <code>Symbol.for('nodejs.rejection')</code></p>
1066<p>See how to write a custom <a href="#events_emitter_symbol_for_nodejs_rejection_err_eventname_args">rejection handler</a>.</p>
1067</section><section><h3><code>events.listenerCount(emitter, eventName)</code><span><a class="mark" href="#events_events_listenercount_emitter_eventname" id="events_events_listenercount_emitter_eventname">#</a></span></h3>
1068<div class="api_metadata">
1069<span>Added in: v0.9.12</span><span>Deprecated since: v3.2.0</span>
1070</div>
1071<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#events_emitter_listenercount_eventname"><code>emitter.listenerCount()</code></a> instead.</div><p></p>
1072<ul>
1073<li><code>emitter</code> <a href="events.html#events_class_eventemitter" class="type">&#x3C;EventEmitter></a> The emitter to query</li>
1074<li><code>eventName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a> The event name</li>
1075</ul>
1076<p>A class method that returns the number of listeners for the given <code>eventName</code>
1077registered on the given <code>emitter</code>.</p>
1078<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitter</span>, listenerCount } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
1079<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>();
1080myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> {});
1081myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> {});
1082<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">listenerCount</span>(myEmitter, <span class="hljs-string">'event'</span>));
1083<span class="hljs-comment">// Prints: 2</span></code></pre>
1084</section><section><h3><code>events.on(emitter, eventName[, options])</code><span><a class="mark" href="#events_events_on_emitter_eventname_options" id="events_events_on_emitter_eventname_options">#</a></span></h3>
1085<div class="api_metadata">
1086<span>Added in: v13.6.0, v12.16.0</span>
1087</div>
1088<ul>
1089<li><code>emitter</code> <a href="events.html#events_class_eventemitter" class="type">&#x3C;EventEmitter></a></li>
1090<li><code>eventName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a> The name of the event being listened for</li>
1091<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1092<ul>
1093<li><code>signal</code> <a href="globals.html#globals_class_abortsignal" class="type">&#x3C;AbortSignal></a> Can be used to cancel awaiting events.</li>
1094</ul>
1095</li>
1096<li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x3C;AsyncIterator></a> that iterates <code>eventName</code> events emitted by the <code>emitter</code></li>
1097</ul>
1098<pre><code class="language-js"><span class="hljs-keyword">const</span> { on, <span class="hljs-title class_">EventEmitter</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
1099
1100(<span class="hljs-keyword">async</span> () => {
1101  <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>();
1102
1103  <span class="hljs-comment">// Emit later on</span>
1104  process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> {
1105    ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'bar'</span>);
1106    ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-number">42</span>);
1107  });
1108
1109  <span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> event <span class="hljs-keyword">of</span> <span class="hljs-title function_">on</span>(ee, <span class="hljs-string">'foo'</span>)) {
1110    <span class="hljs-comment">// The execution of this inner block is synchronous and it</span>
1111    <span class="hljs-comment">// processes one event at a time (even with await). Do not use</span>
1112    <span class="hljs-comment">// if concurrent execution is required.</span>
1113    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event); <span class="hljs-comment">// prints ['bar'] [42]</span>
1114  }
1115  <span class="hljs-comment">// Unreachable here</span>
1116})();</code></pre>
1117<p>Returns an <code>AsyncIterator</code> that iterates <code>eventName</code> events. It will throw
1118if the <code>EventEmitter</code> emits <code>'error'</code>. It removes all listeners when
1119exiting the loop. The <code>value</code> returned by each iteration is an array
1120composed of the emitted event arguments.</p>
1121<p>An <a href="globals.html#globals_class_abortsignal" class="type">&#x3C;AbortSignal></a> can be used to cancel waiting on events:</p>
1122<pre><code class="language-js"><span class="hljs-keyword">const</span> { on, <span class="hljs-title class_">EventEmitter</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
1123<span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>();
1124
1125(<span class="hljs-keyword">async</span> () => {
1126  <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>();
1127
1128  <span class="hljs-comment">// Emit later on</span>
1129  process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> {
1130    ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'bar'</span>);
1131    ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-number">42</span>);
1132  });
1133
1134  <span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> event <span class="hljs-keyword">of</span> <span class="hljs-title function_">on</span>(ee, <span class="hljs-string">'foo'</span>, { <span class="hljs-attr">signal</span>: ac.<span class="hljs-property">signal</span> })) {
1135    <span class="hljs-comment">// The execution of this inner block is synchronous and it</span>
1136    <span class="hljs-comment">// processes one event at a time (even with await). Do not use</span>
1137    <span class="hljs-comment">// if concurrent execution is required.</span>
1138    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event); <span class="hljs-comment">// prints ['bar'] [42]</span>
1139  }
1140  <span class="hljs-comment">// Unreachable here</span>
1141})();
1142
1143process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> ac.<span class="hljs-title function_">abort</span>());</code></pre>
1144</section><section><h3><code>events.setMaxListeners(n[, ...eventTargets])</code><span><a class="mark" href="#events_events_setmaxlisteners_n_eventtargets" id="events_events_setmaxlisteners_n_eventtargets">#</a></span></h3>
1145<div class="api_metadata">
1146<span>Added in: v14.17.0</span>
1147</div>
1148<ul>
1149<li><code>n</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> A non-negative number. The maximum number of listeners per
1150<code>EventTarget</code> event.</li>
1151<li><code>...eventsTargets</code> <a href="events.html#events_class_eventtarget" class="type">&#x3C;EventTarget[]></a> | <a href="events.html#events_class_eventemitter" class="type">&#x3C;EventEmitter[]></a> Zero or more <a href="events.html#events_class_eventtarget" class="type">&#x3C;EventTarget></a>
1152or <a href="events.html#events_class_eventemitter" class="type">&#x3C;EventEmitter></a> instances. If none are specified, <code>n</code> is set as the default
1153max for all newly created <a href="events.html#events_class_eventtarget" class="type">&#x3C;EventTarget></a> and <a href="events.html#events_class_eventemitter" class="type">&#x3C;EventEmitter></a> objects.</li>
1154</ul>
1155<pre><code class="language-js"><span class="hljs-keyword">const</span> {
1156  setMaxListeners,
1157  <span class="hljs-title class_">EventEmitter</span>
1158} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
1159
1160<span class="hljs-keyword">const</span> target = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventTarget</span>();
1161<span class="hljs-keyword">const</span> emitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>();
1162
1163<span class="hljs-title function_">setMaxListeners</span>(<span class="hljs-number">5</span>, target, emitter);</code></pre>
1164<p><a id="event-target-and-event-api"></a></p>
1165</section><section><h3><code>EventTarget</code> and <code>Event</code> API<span><a class="mark" href="#events_eventtarget_and_event_api" id="events_eventtarget_and_event_api">#</a></span></h3>
1166<div class="api_metadata">
1167<span>Added in: v14.5.0</span>
1168</div>
1169<p></p><div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - Experimental</div><p></p>
1170<p>The <code>EventTarget</code> and <code>Event</code> objects are a Node.js-specific implementation
1171of the <a href="https://dom.spec.whatwg.org/#eventtarget"><code>EventTarget</code> Web API</a> that are exposed by some Node.js core APIs.
1172Neither the <code>EventTarget</code> nor <code>Event</code> classes are available for end
1173user code to create.</p>
1174<pre><code class="language-js"><span class="hljs-keyword">const</span> target = <span class="hljs-title function_">getEventTargetSomehow</span>();
1175
1176target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">(<span class="hljs-params">event</span>) =></span> {
1177  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'foo event happened!'</span>);
1178});</code></pre>
1179<h4>Node.js <code>EventTarget</code> vs. DOM <code>EventTarget</code><span><a class="mark" href="#events_node_js_eventtarget_vs_dom_eventtarget" id="events_node_js_eventtarget_vs_dom_eventtarget">#</a></span></h4>
1180<p>There are two key differences between the Node.js <code>EventTarget</code> and the
1181<a href="https://dom.spec.whatwg.org/#eventtarget"><code>EventTarget</code> Web API</a>:</p>
1182<ol>
1183<li>Whereas DOM <code>EventTarget</code> instances <em>may</em> be hierarchical, there is no
1184concept of hierarchy and event propagation in Node.js. That is, an event
1185dispatched to an <code>EventTarget</code> does not propagate through a hierarchy of
1186nested target objects that may each have their own set of handlers for the
1187event.</li>
1188<li>In the Node.js <code>EventTarget</code>, if an event listener is an async function
1189or returns a <code>Promise</code>, and the returned <code>Promise</code> rejects, the rejection
1190is automatically captured and handled the same way as a listener that
1191throws synchronously (see <a href="#events_eventtarget_error_handling"><code>EventTarget</code> error handling</a> for details).</li>
1192</ol>
1193<h4><code>NodeEventTarget</code> vs. <code>EventEmitter</code><span><a class="mark" href="#events_nodeeventtarget_vs_eventemitter" id="events_nodeeventtarget_vs_eventemitter">#</a></span></h4>
1194<p>The <code>NodeEventTarget</code> object implements a modified subset of the
1195<code>EventEmitter</code> API that allows it to closely <em>emulate</em> an <code>EventEmitter</code> in
1196certain situations. A <code>NodeEventTarget</code> is <em>not</em> an instance of <code>EventEmitter</code>
1197and cannot be used in place of an <code>EventEmitter</code> in most cases.</p>
1198<ol>
1199<li>Unlike <code>EventEmitter</code>, any given <code>listener</code> can be registered at most once
1200per event <code>type</code>. Attempts to register a <code>listener</code> multiple times are
1201ignored.</li>
1202<li>The <code>NodeEventTarget</code> does not emulate the full <code>EventEmitter</code> API.
1203Specifically the <code>prependListener()</code>, <code>prependOnceListener()</code>,
1204<code>rawListeners()</code>, <code>setMaxListeners()</code>, <code>getMaxListeners()</code>, and
1205<code>errorMonitor</code> APIs are not emulated. The <code>'newListener'</code> and
1206<code>'removeListener'</code> events will also not be emitted.</li>
1207<li>The <code>NodeEventTarget</code> does not implement any special default behavior
1208for events with type <code>'error'</code>.</li>
1209<li>The <code>NodeEventTarget</code> supports <code>EventListener</code> objects as well as
1210functions as handlers for all event types.</li>
1211</ol>
1212<h4>Event listener<span><a class="mark" href="#events_event_listener" id="events_event_listener">#</a></span></h4>
1213<p>Event listeners registered for an event <code>type</code> may either be JavaScript
1214functions or objects with a <code>handleEvent</code> property whose value is a function.</p>
1215<p>In either case, the handler function is invoked with the <code>event</code> argument
1216passed to the <code>eventTarget.dispatchEvent()</code> function.</p>
1217<p>Async functions may be used as event listeners. If an async handler function
1218rejects, the rejection is captured and handled as described in
1219<a href="#events_eventtarget_error_handling"><code>EventTarget</code> error handling</a>.</p>
1220<p>An error thrown by one handler function does not prevent the other handlers
1221from being invoked.</p>
1222<p>The return value of a handler function is ignored.</p>
1223<p>Handlers are always invoked in the order they were added.</p>
1224<p>Handler functions may mutate the <code>event</code> object.</p>
1225<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">handler1</span>(<span class="hljs-params">event</span>) {
1226  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span class="hljs-property">type</span>);  <span class="hljs-comment">// Prints 'foo'</span>
1227  event.<span class="hljs-property">a</span> = <span class="hljs-number">1</span>;
1228}
1229
1230<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">handler2</span>(<span class="hljs-params">event</span>) {
1231  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span class="hljs-property">type</span>);  <span class="hljs-comment">// Prints 'foo'</span>
1232  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span class="hljs-property">a</span>);  <span class="hljs-comment">// Prints 1</span>
1233}
1234
1235<span class="hljs-keyword">const</span> handler3 = {
1236  <span class="hljs-title function_">handleEvent</span>(<span class="hljs-params">event</span>) {
1237    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span class="hljs-property">type</span>);  <span class="hljs-comment">// Prints 'foo'</span>
1238  }
1239};
1240
1241<span class="hljs-keyword">const</span> handler4 = {
1242  <span class="hljs-keyword">async</span> <span class="hljs-title function_">handleEvent</span>(<span class="hljs-params">event</span>) {
1243    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event.<span class="hljs-property">type</span>);  <span class="hljs-comment">// Prints 'foo'</span>
1244  }
1245};
1246
1247<span class="hljs-keyword">const</span> target = <span class="hljs-title function_">getEventTargetSomehow</span>();
1248
1249target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, handler1);
1250target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, handler2);
1251target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, handler3);
1252target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, handler4, { <span class="hljs-attr">once</span>: <span class="hljs-literal">true</span> });</code></pre>
1253<h4><code>EventTarget</code> error handling<span><a class="mark" href="#events_eventtarget_error_handling" id="events_eventtarget_error_handling">#</a></span></h4>
1254<p>When a registered event listener throws (or returns a Promise that rejects),
1255by default the error is treated as an uncaught exception on
1256<code>process.nextTick()</code>. This means uncaught exceptions in <code>EventTarget</code>s will
1257terminate the Node.js process by default.</p>
1258<p>Throwing within an event listener will <em>not</em> stop the other registered handlers
1259from being invoked.</p>
1260<p>The <code>EventTarget</code> does not implement any special default handling for <code>'error'</code>
1261type events like <code>EventEmitter</code>.</p>
1262<p>Currently errors are first forwarded to the <code>process.on('error')</code> event
1263before reaching <code>process.on('uncaughtException')</code>. This behavior is
1264deprecated and will change in a future release to align <code>EventTarget</code> with
1265other Node.js APIs. Any code relying on the <code>process.on('error')</code> event should
1266be aligned with the new behavior.</p>
1267<h4>Class: <code>Event</code><span><a class="mark" href="#events_class_event" id="events_class_event">#</a></span></h4>
1268<div class="api_metadata">
1269<span>Added in: v14.5.0</span>
1270</div>
1271<p>The <code>Event</code> object is an adaptation of the <a href="https://dom.spec.whatwg.org/#event"><code>Event</code> Web API</a>. Instances
1272are created internally by Node.js.</p>
1273<h5><code>event.bubbles</code><span><a class="mark" href="#events_event_bubbles" id="events_event_bubbles">#</a></span></h5>
1274<div class="api_metadata">
1275<span>Added in: v14.5.0</span>
1276</div>
1277<ul>
1278<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Always returns <code>false</code>.</li>
1279</ul>
1280<p>This is not used in Node.js and is provided purely for completeness.</p>
1281<h5><code>event.cancelBubble()</code><span><a class="mark" href="#events_event_cancelbubble" id="events_event_cancelbubble">#</a></span></h5>
1282<div class="api_metadata">
1283<span>Added in: v14.5.0</span>
1284</div>
1285<p>Alias for <code>event.stopPropagation()</code>. This is not used in Node.js and is
1286provided purely for completeness.</p>
1287<h5><code>event.cancelable</code><span><a class="mark" href="#events_event_cancelable" id="events_event_cancelable">#</a></span></h5>
1288<div class="api_metadata">
1289<span>Added in: v14.5.0</span>
1290</div>
1291<ul>
1292<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> True if the event was created with the <code>cancelable</code> option.</li>
1293</ul>
1294<h5><code>event.composed</code><span><a class="mark" href="#events_event_composed" id="events_event_composed">#</a></span></h5>
1295<div class="api_metadata">
1296<span>Added in: v14.5.0</span>
1297</div>
1298<ul>
1299<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Always returns <code>false</code>.</li>
1300</ul>
1301<p>This is not used in Node.js and is provided purely for completeness.</p>
1302<h5><code>event.composedPath()</code><span><a class="mark" href="#events_event_composedpath" id="events_event_composedpath">#</a></span></h5>
1303<div class="api_metadata">
1304<span>Added in: v14.5.0</span>
1305</div>
1306<p>Returns an array containing the current <code>EventTarget</code> as the only entry or
1307empty if the event is not being dispatched. This is not used in
1308Node.js and is provided purely for completeness.</p>
1309<h5><code>event.currentTarget</code><span><a class="mark" href="#events_event_currenttarget" id="events_event_currenttarget">#</a></span></h5>
1310<div class="api_metadata">
1311<span>Added in: v14.5.0</span>
1312</div>
1313<ul>
1314<li>Type: <a href="events.html#events_class_eventtarget" class="type">&#x3C;EventTarget></a> The <code>EventTarget</code> dispatching the event.</li>
1315</ul>
1316<p>Alias for <code>event.target</code>.</p>
1317<h5><code>event.defaultPrevented</code><span><a class="mark" href="#events_event_defaultprevented" id="events_event_defaultprevented">#</a></span></h5>
1318<div class="api_metadata">
1319<span>Added in: v14.5.0</span>
1320</div>
1321<ul>
1322<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1323</ul>
1324<p>Is <code>true</code> if <code>cancelable</code> is <code>true</code> and <code>event.preventDefault()</code> has been
1325called.</p>
1326<h5><code>event.eventPhase</code><span><a class="mark" href="#events_event_eventphase" id="events_event_eventphase">#</a></span></h5>
1327<div class="api_metadata">
1328<span>Added in: v14.5.0</span>
1329</div>
1330<ul>
1331<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Returns <code>0</code> while an event is not being dispatched, <code>2</code> while
1332it is being dispatched.</li>
1333</ul>
1334<p>This is not used in Node.js and is provided purely for completeness.</p>
1335<h5><code>event.isTrusted</code><span><a class="mark" href="#events_event_istrusted" id="events_event_istrusted">#</a></span></h5>
1336<div class="api_metadata">
1337<span>Added in: v14.5.0</span>
1338</div>
1339<ul>
1340<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1341</ul>
1342<p>The <a href="globals.html#globals_class_abortsignal" class="type">&#x3C;AbortSignal></a> <code>"abort"</code> event is emitted with <code>isTrusted</code> set to <code>true</code>. The
1343value is <code>false</code> in all other cases.</p>
1344<h5><code>event.preventDefault()</code><span><a class="mark" href="#events_event_preventdefault" id="events_event_preventdefault">#</a></span></h5>
1345<div class="api_metadata">
1346<span>Added in: v14.5.0</span>
1347</div>
1348<p>Sets the <code>defaultPrevented</code> property to <code>true</code> if <code>cancelable</code> is <code>true</code>.</p>
1349<h5><code>event.returnValue</code><span><a class="mark" href="#events_event_returnvalue" id="events_event_returnvalue">#</a></span></h5>
1350<div class="api_metadata">
1351<span>Added in: v14.5.0</span>
1352</div>
1353<ul>
1354<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> True if the event has not been canceled.</li>
1355</ul>
1356<p>This is not used in Node.js and is provided purely for completeness.</p>
1357<h5><code>event.srcElement</code><span><a class="mark" href="#events_event_srcelement" id="events_event_srcelement">#</a></span></h5>
1358<div class="api_metadata">
1359<span>Added in: v14.5.0</span>
1360</div>
1361<ul>
1362<li>Type: <a href="events.html#events_class_eventtarget" class="type">&#x3C;EventTarget></a> The <code>EventTarget</code> dispatching the event.</li>
1363</ul>
1364<p>Alias for <code>event.target</code>.</p>
1365<h5><code>event.stopImmediatePropagation()</code><span><a class="mark" href="#events_event_stopimmediatepropagation" id="events_event_stopimmediatepropagation">#</a></span></h5>
1366<div class="api_metadata">
1367<span>Added in: v14.5.0</span>
1368</div>
1369<p>Stops the invocation of event listeners after the current one completes.</p>
1370<h5><code>event.stopPropagation()</code><span><a class="mark" href="#events_event_stoppropagation" id="events_event_stoppropagation">#</a></span></h5>
1371<div class="api_metadata">
1372<span>Added in: v14.5.0</span>
1373</div>
1374<p>This is not used in Node.js and is provided purely for completeness.</p>
1375<h5><code>event.target</code><span><a class="mark" href="#events_event_target" id="events_event_target">#</a></span></h5>
1376<div class="api_metadata">
1377<span>Added in: v14.5.0</span>
1378</div>
1379<ul>
1380<li>Type: <a href="events.html#events_class_eventtarget" class="type">&#x3C;EventTarget></a> The <code>EventTarget</code> dispatching the event.</li>
1381</ul>
1382<h5><code>event.timeStamp</code><span><a class="mark" href="#events_event_timestamp" id="events_event_timestamp">#</a></span></h5>
1383<div class="api_metadata">
1384<span>Added in: v14.5.0</span>
1385</div>
1386<ul>
1387<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1388</ul>
1389<p>The millisecond timestamp when the <code>Event</code> object was created.</p>
1390<h5><code>event.type</code><span><a class="mark" href="#events_event_type" id="events_event_type">#</a></span></h5>
1391<div class="api_metadata">
1392<span>Added in: v14.5.0</span>
1393</div>
1394<ul>
1395<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1396</ul>
1397<p>The event type identifier.</p>
1398<h4>Class: <code>EventTarget</code><span><a class="mark" href="#events_class_eventtarget" id="events_class_eventtarget">#</a></span></h4>
1399<div class="api_metadata">
1400<span>Added in: v14.5.0</span>
1401</div>
1402<h5><code>eventTarget.addEventListener(type, listener[, options])</code><span><a class="mark" href="#events_eventtarget_addeventlistener_type_listener_options" id="events_eventtarget_addeventlistener_type_listener_options">#</a></span></h5>
1403<div class="api_metadata">
1404<span>Added in: v14.5.0</span>
1405</div>
1406<ul>
1407<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1408<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> | <a href="events.html#events_event_listener" class="type">&#x3C;EventListener></a></li>
1409<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1410<ul>
1411<li><code>once</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, the listener is automatically removed
1412when it is first invoked. <strong>Default:</strong> <code>false</code>.</li>
1413<li><code>passive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> When <code>true</code>, serves as a hint that the listener will
1414not call the <code>Event</code> object's <code>preventDefault()</code> method.
1415<strong>Default:</strong> <code>false</code>.</li>
1416<li><code>capture</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Not directly used by Node.js. Added for API
1417completeness. <strong>Default:</strong> <code>false</code>.</li>
1418</ul>
1419</li>
1420</ul>
1421<p>Adds a new handler for the <code>type</code> event. Any given <code>listener</code> is added
1422only once per <code>type</code> and per <code>capture</code> option value.</p>
1423<p>If the <code>once</code> option is <code>true</code>, the <code>listener</code> is removed after the
1424next time a <code>type</code> event is dispatched.</p>
1425<p>The <code>capture</code> option is not used by Node.js in any functional way other than
1426tracking registered event listeners per the <code>EventTarget</code> specification.
1427Specifically, the <code>capture</code> option is used as part of the key when registering
1428a <code>listener</code>. Any individual <code>listener</code> may be added once with
1429<code>capture = false</code>, and once with <code>capture = true</code>.</p>
1430<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">handler</span>(<span class="hljs-params">event</span>) {}
1431
1432<span class="hljs-keyword">const</span> target = <span class="hljs-title function_">getEventTargetSomehow</span>();
1433target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, handler, { <span class="hljs-attr">capture</span>: <span class="hljs-literal">true</span> });  <span class="hljs-comment">// first</span>
1434target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, handler, { <span class="hljs-attr">capture</span>: <span class="hljs-literal">false</span> }); <span class="hljs-comment">// second</span>
1435
1436<span class="hljs-comment">// Removes the second instance of handler</span>
1437target.<span class="hljs-title function_">removeEventListener</span>(<span class="hljs-string">'foo'</span>, handler);
1438
1439<span class="hljs-comment">// Removes the first instance of handler</span>
1440target.<span class="hljs-title function_">removeEventListener</span>(<span class="hljs-string">'foo'</span>, handler, { <span class="hljs-attr">capture</span>: <span class="hljs-literal">true</span> });</code></pre>
1441<h5><code>eventTarget.dispatchEvent(event)</code><span><a class="mark" href="#events_eventtarget_dispatchevent_event" id="events_eventtarget_dispatchevent_event">#</a></span></h5>
1442<div class="api_metadata">
1443<span>Added in: v14.5.0</span>
1444</div>
1445<ul>
1446<li><code>event</code> <a href="events.html#events_class_event" class="type">&#x3C;Event></a></li>
1447<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> if either event’s <code>cancelable</code> attribute value is
1448false or its <code>preventDefault()</code> method was not invoked, otherwise <code>false</code>.</li>
1449</ul>
1450<p>Dispatches the <code>event</code> to the list of handlers for <code>event.type</code>.</p>
1451<p>The registered event listeners is synchronously invoked in the order they
1452were registered.</p>
1453<h5><code>eventTarget.removeEventListener(type, listener)</code><span><a class="mark" href="#events_eventtarget_removeeventlistener_type_listener" id="events_eventtarget_removeeventlistener_type_listener">#</a></span></h5>
1454<div class="api_metadata">
1455<span>Added in: v14.5.0</span>
1456</div>
1457<ul>
1458<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1459<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> | <a href="events.html#events_event_listener" class="type">&#x3C;EventListener></a></li>
1460<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
1461<ul>
1462<li><code>capture</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1463</ul>
1464</li>
1465</ul>
1466<p>Removes the <code>listener</code> from the list of handlers for event <code>type</code>.</p>
1467<h4>Class: <code>NodeEventTarget</code><span><a class="mark" href="#events_class_nodeeventtarget" id="events_class_nodeeventtarget">#</a></span></h4>
1468<div class="api_metadata">
1469<span>Added in: v14.5.0</span>
1470</div>
1471<ul>
1472<li>Extends: <a href="events.html#events_class_eventtarget" class="type">&#x3C;EventTarget></a></li>
1473</ul>
1474<p>The <code>NodeEventTarget</code> is a Node.js-specific extension to <code>EventTarget</code>
1475that emulates a subset of the <code>EventEmitter</code> API.</p>
1476<h5><code>nodeEventTarget.addListener(type, listener[, options])</code><span><a class="mark" href="#events_nodeeventtarget_addlistener_type_listener_options" id="events_nodeeventtarget_addlistener_type_listener_options">#</a></span></h5>
1477<div class="api_metadata">
1478<span>Added in: v14.5.0</span>
1479</div>
1480<ul>
1481<li>
1482<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></p>
1483</li>
1484<li>
1485<p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> | <a href="events.html#events_event_listener" class="type">&#x3C;EventListener></a></p>
1486</li>
1487<li>
1488<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></p>
1489<ul>
1490<li><code>once</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1491</ul>
1492</li>
1493<li>
1494<p>Returns: <a href="events.html#events_class_eventtarget" class="type">&#x3C;EventTarget></a> this</p>
1495</li>
1496</ul>
1497<p>Node.js-specific extension to the <code>EventTarget</code> class that emulates the
1498equivalent <code>EventEmitter</code> API. The only difference between <code>addListener()</code> and
1499<code>addEventListener()</code> is that <code>addListener()</code> will return a reference to the
1500<code>EventTarget</code>.</p>
1501<h5><code>nodeEventTarget.eventNames()</code><span><a class="mark" href="#events_nodeeventtarget_eventnames" id="events_nodeeventtarget_eventnames">#</a></span></h5>
1502<div class="api_metadata">
1503<span>Added in: v14.5.0</span>
1504</div>
1505<ul>
1506<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
1507</ul>
1508<p>Node.js-specific extension to the <code>EventTarget</code> class that returns an array
1509of event <code>type</code> names for which event listeners are registered.</p>
1510<h5><code>nodeEventTarget.listenerCount(type)</code><span><a class="mark" href="#events_nodeeventtarget_listenercount_type" id="events_nodeeventtarget_listenercount_type">#</a></span></h5>
1511<div class="api_metadata">
1512<span>Added in: v14.5.0</span>
1513</div>
1514<ul>
1515<li>
1516<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></p>
1517</li>
1518<li>
1519<p>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></p>
1520</li>
1521</ul>
1522<p>Node.js-specific extension to the <code>EventTarget</code> class that returns the number
1523of event listeners registered for the <code>type</code>.</p>
1524<h5><code>nodeEventTarget.off(type, listener)</code><span><a class="mark" href="#events_nodeeventtarget_off_type_listener" id="events_nodeeventtarget_off_type_listener">#</a></span></h5>
1525<div class="api_metadata">
1526<span>Added in: v14.5.0</span>
1527</div>
1528<ul>
1529<li>
1530<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></p>
1531</li>
1532<li>
1533<p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> | <a href="events.html#events_event_listener" class="type">&#x3C;EventListener></a></p>
1534</li>
1535<li>
1536<p>Returns: <a href="events.html#events_class_eventtarget" class="type">&#x3C;EventTarget></a> this</p>
1537</li>
1538</ul>
1539<p>Node.js-specific alias for <code>eventTarget.removeListener()</code>.</p>
1540<h5><code>nodeEventTarget.on(type, listener[, options])</code><span><a class="mark" href="#events_nodeeventtarget_on_type_listener_options" id="events_nodeeventtarget_on_type_listener_options">#</a></span></h5>
1541<div class="api_metadata">
1542<span>Added in: v14.5.0</span>
1543</div>
1544<ul>
1545<li>
1546<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></p>
1547</li>
1548<li>
1549<p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> | <a href="events.html#events_event_listener" class="type">&#x3C;EventListener></a></p>
1550</li>
1551<li>
1552<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></p>
1553<ul>
1554<li><code>once</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1555</ul>
1556</li>
1557<li>
1558<p>Returns: <a href="events.html#events_class_eventtarget" class="type">&#x3C;EventTarget></a> this</p>
1559</li>
1560</ul>
1561<p>Node.js-specific alias for <code>eventTarget.addListener()</code>.</p>
1562<h5><code>nodeEventTarget.once(type, listener[, options])</code><span><a class="mark" href="#events_nodeeventtarget_once_type_listener_options" id="events_nodeeventtarget_once_type_listener_options">#</a></span></h5>
1563<div class="api_metadata">
1564<span>Added in: v14.5.0</span>
1565</div>
1566<ul>
1567<li>
1568<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></p>
1569</li>
1570<li>
1571<p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> | <a href="events.html#events_event_listener" class="type">&#x3C;EventListener></a></p>
1572</li>
1573<li>
1574<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></p>
1575</li>
1576<li>
1577<p>Returns: <a href="events.html#events_class_eventtarget" class="type">&#x3C;EventTarget></a> this</p>
1578</li>
1579</ul>
1580<p>Node.js-specific extension to the <code>EventTarget</code> class that adds a <code>once</code>
1581listener for the given event <code>type</code>. This is equivalent to calling <code>on</code>
1582with the <code>once</code> option set to <code>true</code>.</p>
1583<h5><code>nodeEventTarget.removeAllListeners([type])</code><span><a class="mark" href="#events_nodeeventtarget_removealllisteners_type" id="events_nodeeventtarget_removealllisteners_type">#</a></span></h5>
1584<div class="api_metadata">
1585<span>Added in: v14.5.0</span>
1586</div>
1587<ul>
1588<li>
1589<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></p>
1590</li>
1591<li>
1592<p>Returns: <a href="events.html#events_class_eventtarget" class="type">&#x3C;EventTarget></a> this</p>
1593</li>
1594</ul>
1595<p>Node.js-specific extension to the <code>EventTarget</code> class. If <code>type</code> is specified,
1596removes all registered listeners for <code>type</code>, otherwise removes all registered
1597listeners.</p>
1598<h5><code>nodeEventTarget.removeListener(type, listener)</code><span><a class="mark" href="#events_nodeeventtarget_removelistener_type_listener" id="events_nodeeventtarget_removelistener_type_listener">#</a></span></h5>
1599<div class="api_metadata">
1600<span>Added in: v14.5.0</span>
1601</div>
1602<ul>
1603<li>
1604<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></p>
1605</li>
1606<li>
1607<p><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> | <a href="events.html#events_event_listener" class="type">&#x3C;EventListener></a></p>
1608</li>
1609<li>
1610<p>Returns: <a href="events.html#events_class_eventtarget" class="type">&#x3C;EventTarget></a> this</p>
1611</li>
1612</ul>
1613<p>Node.js-specific extension to the <code>EventTarget</code> class that removes the
1614<code>listener</code> for the given <code>type</code>. The only difference between <code>removeListener()</code>
1615and <code>removeEventListener()</code> is that <code>removeListener()</code> will return a reference
1616to the <code>EventTarget</code>.</p></section>
1617        <!-- API END -->
1618      </div>
1619    </div>
1620  </div>
1621  <script>
1622    'use strict';
1623    {
1624      const kCustomPreference = 'customDarkTheme';
1625      const userSettings = sessionStorage.getItem(kCustomPreference);
1626      const themeToggleButton = document.getElementById('theme-toggle-btn');
1627      if (userSettings === null && window.matchMedia) {
1628        const mq = window.matchMedia('(prefers-color-scheme: dark)');
1629        if ('onchange' in mq) {
1630          function mqChangeListener(e) {
1631            document.body.classList.toggle('dark-mode', e.matches);
1632          }
1633          mq.addEventListener('change', mqChangeListener);
1634          if (themeToggleButton) {
1635            themeToggleButton.addEventListener('click', function() {
1636              mq.removeEventListener('change', mqChangeListener);
1637            }, { once: true });
1638          }
1639        }
1640        if (mq.matches) {
1641          document.body.classList.add('dark-mode');
1642        }
1643      } else if (userSettings === 'true') {
1644        document.body.classList.add('dark-mode');
1645      }
1646      if (themeToggleButton) {
1647        themeToggleButton.hidden = false;
1648        themeToggleButton.addEventListener('click', function() {
1649          sessionStorage.setItem(
1650            kCustomPreference,
1651            document.body.classList.toggle('dark-mode')
1652          );
1653        });
1654      }
1655    }
1656  </script>
1657</body>
1658</html>
1659