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