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