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="v18.18.2"> 7 <title>Events | Node.js v18.18.2 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/events.html"> 12 <script async defer src="assets/api.js" type="text/javascript"></script> 13 <style>@media(max-width:574px){.with-44-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:718px){.with-62-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:686px){.with-58-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:566px){.with-43-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:1072px){.with-72-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:758px){.with-67-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:1152px){.with-82-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:654px){.with-54-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:726px){.with-63-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:638px){.with-52-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:702px){.with-60-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:1096px){.with-75-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style> 14</head> 15<body class="alt apidoc" id="api-section-events"> 16 <div id="content" class="clearfix"> 17 <div id="column2" class="interior"> 18 <div id="intro" class="interior"> 19 <a href="/" title="Go back to the home page"> 20 Node.js 21 </a> 22 </div> 23 <ul> 24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 26</ul> 27<hr class="line"> 28<ul> 29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 33<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 38<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 39<li><a href="console.html" class="nav-console">Console</a></li> 40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 45<li><a href="dns.html" class="nav-dns">DNS</a></li> 46<li><a href="domain.html" class="nav-domain">Domain</a></li> 47<li><a href="errors.html" class="nav-errors">Errors</a></li> 48<li><a href="events.html" class="nav-events active">Events</a></li> 49<li><a href="fs.html" class="nav-fs">File system</a></li> 50<li><a href="globals.html" class="nav-globals">Globals</a></li> 51<li><a href="http.html" class="nav-http">HTTP</a></li> 52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 53<li><a href="https.html" class="nav-https">HTTPS</a></li> 54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 55<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 60<li><a href="net.html" class="nav-net">Net</a></li> 61<li><a href="os.html" class="nav-os">OS</a></li> 62<li><a href="path.html" class="nav-path">Path</a></li> 63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 65<li><a href="process.html" class="nav-process">Process</a></li> 66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 68<li><a href="readline.html" class="nav-readline">Readline</a></li> 69<li><a href="repl.html" class="nav-repl">REPL</a></li> 70<li><a href="report.html" class="nav-report">Report</a></li> 71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 72<li><a href="stream.html" class="nav-stream">Stream</a></li> 73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 74<li><a href="test.html" class="nav-test">Test runner</a></li> 75<li><a href="timers.html" class="nav-timers">Timers</a></li> 76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 78<li><a href="tty.html" class="nav-tty">TTY</a></li> 79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 80<li><a href="url.html" class="nav-url">URL</a></li> 81<li><a href="util.html" class="nav-util">Utilities</a></li> 82<li><a href="v8.html" class="nav-v8">V8</a></li> 83<li><a href="vm.html" class="nav-vm">VM</a></li> 84<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 89</ul> 90<hr class="line"> 91<ul> 92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 93</ul> 94 </div> 95 96 <div id="column1" data-id="events" class="interior"> 97 <header class="header"> 98 <div class="header-container"> 99 <h1>Node.js v18.18.2 documentation</h1> 100 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 101 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 102 <path fill="none" d="M0 0h24v24H0z" /> 103 <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"/> 104 <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"/> 105 </svg> 106 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 107 <path d="M0 0h24v24H0z" fill="none" /> 108 <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"/> 109 </svg> 110 </button> 111 </div> 112 <div id="gtoc"> 113 <ul> 114 <li class="pinned-header">Node.js v18.18.2</li> 115 116 <li class="picker-header"> 117 <a href="#"> 118 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 119 Table of contents 120 </a> 121 122 <div class="picker"><div class="toc"><ul> 123<li><span class="stability_2"><a href="#events">Events</a></span> 124<ul> 125<li><a href="#passing-arguments-and-this-to-listeners">Passing arguments and <code>this</code> to listeners</a></li> 126<li><a href="#asynchronous-vs-synchronous">Asynchronous vs. synchronous</a></li> 127<li><a href="#handling-events-only-once">Handling events only once</a></li> 128<li><a href="#error-events">Error events</a></li> 129<li><a href="#capture-rejections-of-promises">Capture rejections of promises</a></li> 130<li><a href="#class-eventemitter">Class: <code>EventEmitter</code></a> 131<ul> 132<li><a href="#event-newlistener">Event: <code>'newListener'</code></a></li> 133<li><a href="#event-removelistener">Event: <code>'removeListener'</code></a></li> 134<li><a href="#emitteraddlistenereventname-listener"><code>emitter.addListener(eventName, listener)</code></a></li> 135<li><a href="#emitteremiteventname-args"><code>emitter.emit(eventName[, ...args])</code></a></li> 136<li><a href="#emittereventnames"><code>emitter.eventNames()</code></a></li> 137<li><a href="#emittergetmaxlisteners"><code>emitter.getMaxListeners()</code></a></li> 138<li><a href="#emitterlistenercounteventname-listener"><code>emitter.listenerCount(eventName[, listener])</code></a></li> 139<li><a href="#emitterlistenerseventname"><code>emitter.listeners(eventName)</code></a></li> 140<li><a href="#emitteroffeventname-listener"><code>emitter.off(eventName, listener)</code></a></li> 141<li><a href="#emitteroneventname-listener"><code>emitter.on(eventName, listener)</code></a></li> 142<li><a href="#emitteronceeventname-listener"><code>emitter.once(eventName, listener)</code></a></li> 143<li><a href="#emitterprependlistenereventname-listener"><code>emitter.prependListener(eventName, listener)</code></a></li> 144<li><a href="#emitterprependoncelistenereventname-listener"><code>emitter.prependOnceListener(eventName, listener)</code></a></li> 145<li><a href="#emitterremovealllistenerseventname"><code>emitter.removeAllListeners([eventName])</code></a></li> 146<li><a href="#emitterremovelistenereventname-listener"><code>emitter.removeListener(eventName, listener)</code></a></li> 147<li><a href="#emittersetmaxlistenersn"><code>emitter.setMaxListeners(n)</code></a></li> 148<li><a href="#emitterrawlistenerseventname"><code>emitter.rawListeners(eventName)</code></a></li> 149<li><a href="#emittersymbolfornodejsrejectionerr-eventname-args"><code>emitter[Symbol.for('nodejs.rejection')](err, eventName[, ...args])</code></a></li> 150</ul> 151</li> 152<li><a href="#eventsdefaultmaxlisteners"><code>events.defaultMaxListeners</code></a></li> 153<li><a href="#eventserrormonitor"><code>events.errorMonitor</code></a></li> 154<li><a href="#eventsgeteventlistenersemitterortarget-eventname"><code>events.getEventListeners(emitterOrTarget, eventName)</code></a></li> 155<li><a href="#eventsgetmaxlistenersemitterortarget"><code>events.getMaxListeners(emitterOrTarget)</code></a></li> 156<li><a href="#eventsonceemitter-name-options"><code>events.once(emitter, name[, options])</code></a> 157<ul> 158<li><a href="#awaiting-multiple-events-emitted-on-processnexttick">Awaiting multiple events emitted on <code>process.nextTick()</code></a></li> 159</ul> 160</li> 161<li><a href="#eventscapturerejections"><code>events.captureRejections</code></a></li> 162<li><a href="#eventscapturerejectionsymbol"><code>events.captureRejectionSymbol</code></a></li> 163<li><span class="stability_0"><a href="#eventslistenercountemitter-eventname"><code>events.listenerCount(emitter, eventName)</code></a></span></li> 164<li><a href="#eventsonemitter-eventname-options"><code>events.on(emitter, eventName[, options])</code></a></li> 165<li><a href="#eventssetmaxlistenersn-eventtargets"><code>events.setMaxListeners(n[, ...eventTargets])</code></a></li> 166<li><span class="stability_1"><a href="#eventsaddabortlistenersignal-resource"><code>events.addAbortListener(signal, resource)</code></a></span></li> 167<li><a href="#class-eventseventemitterasyncresource-extends-eventemitter">Class: <code>events.EventEmitterAsyncResource extends EventEmitter</code></a> 168<ul> 169<li><a href="#new-eventseventemitterasyncresourceoptions"><code>new events.EventEmitterAsyncResource([options])</code></a></li> 170<li><a href="#eventemitterasyncresourceasyncid"><code>eventemitterasyncresource.asyncId</code></a></li> 171<li><a href="#eventemitterasyncresourceasyncresource"><code>eventemitterasyncresource.asyncResource</code></a></li> 172<li><a href="#eventemitterasyncresourceemitdestroy"><code>eventemitterasyncresource.emitDestroy()</code></a></li> 173<li><a href="#eventemitterasyncresourcetriggerasyncid"><code>eventemitterasyncresource.triggerAsyncId</code></a></li> 174</ul> 175</li> 176<li><a href="#eventtarget-and-event-api"><code>EventTarget</code> and <code>Event</code> API</a> 177<ul> 178<li><a href="#nodejs-eventtarget-vs-dom-eventtarget">Node.js <code>EventTarget</code> vs. DOM <code>EventTarget</code></a></li> 179<li><a href="#nodeeventtarget-vs-eventemitter"><code>NodeEventTarget</code> vs. <code>EventEmitter</code></a></li> 180<li><a href="#event-listener">Event listener</a></li> 181<li><a href="#eventtarget-error-handling"><code>EventTarget</code> error handling</a></li> 182<li><a href="#class-event">Class: <code>Event</code></a> 183<ul> 184<li><a href="#eventbubbles"><code>event.bubbles</code></a></li> 185<li><span class="stability_3"><a href="#eventcancelbubble"><code>event.cancelBubble</code></a></span></li> 186<li><a href="#eventcancelable"><code>event.cancelable</code></a></li> 187<li><a href="#eventcomposed"><code>event.composed</code></a></li> 188<li><a href="#eventcomposedpath"><code>event.composedPath()</code></a></li> 189<li><a href="#eventcurrenttarget"><code>event.currentTarget</code></a></li> 190<li><a href="#eventdefaultprevented"><code>event.defaultPrevented</code></a></li> 191<li><a href="#eventeventphase"><code>event.eventPhase</code></a></li> 192<li><a href="#eventistrusted"><code>event.isTrusted</code></a></li> 193<li><a href="#eventpreventdefault"><code>event.preventDefault()</code></a></li> 194<li><span class="stability_3"><a href="#eventreturnvalue"><code>event.returnValue</code></a></span></li> 195<li><span class="stability_3"><a href="#eventsrcelement"><code>event.srcElement</code></a></span></li> 196<li><a href="#eventstopimmediatepropagation"><code>event.stopImmediatePropagation()</code></a></li> 197<li><a href="#eventstoppropagation"><code>event.stopPropagation()</code></a></li> 198<li><a href="#eventtarget"><code>event.target</code></a></li> 199<li><a href="#eventtimestamp"><code>event.timeStamp</code></a></li> 200<li><a href="#eventtype"><code>event.type</code></a></li> 201</ul> 202</li> 203<li><a href="#class-eventtarget">Class: <code>EventTarget</code></a> 204<ul> 205<li><a href="#eventtargetaddeventlistenertype-listener-options"><code>eventTarget.addEventListener(type, listener[, options])</code></a></li> 206<li><a href="#eventtargetdispatcheventevent"><code>eventTarget.dispatchEvent(event)</code></a></li> 207<li><a href="#eventtargetremoveeventlistenertype-listener-options"><code>eventTarget.removeEventListener(type, listener[, options])</code></a></li> 208</ul> 209</li> 210<li><span class="stability_1"><a href="#class-customevent">Class: <code>CustomEvent</code></a></span> 211<ul> 212<li><span class="stability_1"><a href="#eventdetail"><code>event.detail</code></a></span></li> 213</ul> 214</li> 215<li><a href="#class-nodeeventtarget">Class: <code>NodeEventTarget</code></a> 216<ul> 217<li><a href="#nodeeventtargetaddlistenertype-listener"><code>nodeEventTarget.addListener(type, listener)</code></a></li> 218<li><a href="#nodeeventtargetemittype-arg"><code>nodeEventTarget.emit(type, arg)</code></a></li> 219<li><a href="#nodeeventtargeteventnames"><code>nodeEventTarget.eventNames()</code></a></li> 220<li><a href="#nodeeventtargetlistenercounttype"><code>nodeEventTarget.listenerCount(type)</code></a></li> 221<li><a href="#nodeeventtargetsetmaxlistenersn"><code>nodeEventTarget.setMaxListeners(n)</code></a></li> 222<li><a href="#nodeeventtargetgetmaxlisteners"><code>nodeEventTarget.getMaxListeners()</code></a></li> 223<li><a href="#nodeeventtargetofftype-listener-options"><code>nodeEventTarget.off(type, listener[, options])</code></a></li> 224<li><a href="#nodeeventtargetontype-listener"><code>nodeEventTarget.on(type, listener)</code></a></li> 225<li><a href="#nodeeventtargetoncetype-listener"><code>nodeEventTarget.once(type, listener)</code></a></li> 226<li><a href="#nodeeventtargetremovealllistenerstype"><code>nodeEventTarget.removeAllListeners([type])</code></a></li> 227<li><a href="#nodeeventtargetremovelistenertype-listener-options"><code>nodeEventTarget.removeListener(type, listener[, options])</code></a></li> 228</ul> 229</li> 230</ul> 231</li> 232</ul> 233</li> 234</ul></div></div> 235 </li> 236 237 238 <li class="picker-header"> 239 <a href="#"> 240 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 241 Index 242 </a> 243 244 <div class="picker"><ul> 245<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 246<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 247 248 <li> 249 <a href="index.html">Index</a> 250 </li> 251 </ul> 252 253<hr class="line"> 254<ul> 255<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 256<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 257<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 258<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 259<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 260<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 261<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 262<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 263<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 264<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 265<li><a href="console.html" class="nav-console">Console</a></li> 266<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 267<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 268<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 269<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 270<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 271<li><a href="dns.html" class="nav-dns">DNS</a></li> 272<li><a href="domain.html" class="nav-domain">Domain</a></li> 273<li><a href="errors.html" class="nav-errors">Errors</a></li> 274<li><a href="events.html" class="nav-events active">Events</a></li> 275<li><a href="fs.html" class="nav-fs">File system</a></li> 276<li><a href="globals.html" class="nav-globals">Globals</a></li> 277<li><a href="http.html" class="nav-http">HTTP</a></li> 278<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 279<li><a href="https.html" class="nav-https">HTTPS</a></li> 280<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 281<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 282<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 283<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 284<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 285<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 286<li><a href="net.html" class="nav-net">Net</a></li> 287<li><a href="os.html" class="nav-os">OS</a></li> 288<li><a href="path.html" class="nav-path">Path</a></li> 289<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 290<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 291<li><a href="process.html" class="nav-process">Process</a></li> 292<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 293<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 294<li><a href="readline.html" class="nav-readline">Readline</a></li> 295<li><a href="repl.html" class="nav-repl">REPL</a></li> 296<li><a href="report.html" class="nav-report">Report</a></li> 297<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 298<li><a href="stream.html" class="nav-stream">Stream</a></li> 299<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 300<li><a href="test.html" class="nav-test">Test runner</a></li> 301<li><a href="timers.html" class="nav-timers">Timers</a></li> 302<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 303<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 304<li><a href="tty.html" class="nav-tty">TTY</a></li> 305<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 306<li><a href="url.html" class="nav-url">URL</a></li> 307<li><a href="util.html" class="nav-util">Utilities</a></li> 308<li><a href="v8.html" class="nav-v8">V8</a></li> 309<li><a href="vm.html" class="nav-vm">VM</a></li> 310<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 311<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 312<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 313<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 314<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 315</ul> 316<hr class="line"> 317<ul> 318<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 319</ul></div> 320 </li> 321 322 323 <li class="picker-header"> 324 <a href="#"> 325 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 326 Other versions 327 </a> 328 <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v20.x/api/events.html">20.x</a></li> 329<li><a href="https://nodejs.org/docs/latest-v19.x/api/events.html">19.x</a></li> 330<li><a href="https://nodejs.org/docs/latest-v18.x/api/events.html">18.x <b>LTS</b></a></li> 331<li><a href="https://nodejs.org/docs/latest-v17.x/api/events.html">17.x</a></li> 332<li><a href="https://nodejs.org/docs/latest-v16.x/api/events.html">16.x</a></li> 333<li><a href="https://nodejs.org/docs/latest-v15.x/api/events.html">15.x</a></li> 334<li><a href="https://nodejs.org/docs/latest-v14.x/api/events.html">14.x</a></li> 335<li><a href="https://nodejs.org/docs/latest-v13.x/api/events.html">13.x</a></li> 336<li><a href="https://nodejs.org/docs/latest-v12.x/api/events.html">12.x</a></li> 337<li><a href="https://nodejs.org/docs/latest-v11.x/api/events.html">11.x</a></li> 338<li><a href="https://nodejs.org/docs/latest-v10.x/api/events.html">10.x</a></li> 339<li><a href="https://nodejs.org/docs/latest-v9.x/api/events.html">9.x</a></li> 340<li><a href="https://nodejs.org/docs/latest-v8.x/api/events.html">8.x</a></li> 341<li><a href="https://nodejs.org/docs/latest-v7.x/api/events.html">7.x</a></li> 342<li><a href="https://nodejs.org/docs/latest-v6.x/api/events.html">6.x</a></li> 343<li><a href="https://nodejs.org/docs/latest-v5.x/api/events.html">5.x</a></li> 344<li><a href="https://nodejs.org/docs/latest-v4.x/api/events.html">4.x</a></li> 345<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/events.html">0.12.x</a></li> 346<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/events.html">0.10.x</a></li></ol></div> 347 </li> 348 349 <li class="picker-header"> 350 <a href="#"> 351 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 352 Options 353 </a> 354 355 <div class="picker"> 356 <ul> 357 <li> 358 <a href="all.html">View on single page</a> 359 </li> 360 <li> 361 <a href="events.json">View as JSON</a> 362 </li> 363 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/events.md">Edit on GitHub</a></li> 364 </ul> 365 </div> 366 </li> 367 </ul> 368 </div> 369 <hr> 370 </header> 371 372 <details id="toc" open><summary>Table of contents</summary><ul> 373<li><span class="stability_2"><a href="#events">Events</a></span> 374<ul> 375<li><a href="#passing-arguments-and-this-to-listeners">Passing arguments and <code>this</code> to listeners</a></li> 376<li><a href="#asynchronous-vs-synchronous">Asynchronous vs. synchronous</a></li> 377<li><a href="#handling-events-only-once">Handling events only once</a></li> 378<li><a href="#error-events">Error events</a></li> 379<li><a href="#capture-rejections-of-promises">Capture rejections of promises</a></li> 380<li><a href="#class-eventemitter">Class: <code>EventEmitter</code></a> 381<ul> 382<li><a href="#event-newlistener">Event: <code>'newListener'</code></a></li> 383<li><a href="#event-removelistener">Event: <code>'removeListener'</code></a></li> 384<li><a href="#emitteraddlistenereventname-listener"><code>emitter.addListener(eventName, listener)</code></a></li> 385<li><a href="#emitteremiteventname-args"><code>emitter.emit(eventName[, ...args])</code></a></li> 386<li><a href="#emittereventnames"><code>emitter.eventNames()</code></a></li> 387<li><a href="#emittergetmaxlisteners"><code>emitter.getMaxListeners()</code></a></li> 388<li><a href="#emitterlistenercounteventname-listener"><code>emitter.listenerCount(eventName[, listener])</code></a></li> 389<li><a href="#emitterlistenerseventname"><code>emitter.listeners(eventName)</code></a></li> 390<li><a href="#emitteroffeventname-listener"><code>emitter.off(eventName, listener)</code></a></li> 391<li><a href="#emitteroneventname-listener"><code>emitter.on(eventName, listener)</code></a></li> 392<li><a href="#emitteronceeventname-listener"><code>emitter.once(eventName, listener)</code></a></li> 393<li><a href="#emitterprependlistenereventname-listener"><code>emitter.prependListener(eventName, listener)</code></a></li> 394<li><a href="#emitterprependoncelistenereventname-listener"><code>emitter.prependOnceListener(eventName, listener)</code></a></li> 395<li><a href="#emitterremovealllistenerseventname"><code>emitter.removeAllListeners([eventName])</code></a></li> 396<li><a href="#emitterremovelistenereventname-listener"><code>emitter.removeListener(eventName, listener)</code></a></li> 397<li><a href="#emittersetmaxlistenersn"><code>emitter.setMaxListeners(n)</code></a></li> 398<li><a href="#emitterrawlistenerseventname"><code>emitter.rawListeners(eventName)</code></a></li> 399<li><a href="#emittersymbolfornodejsrejectionerr-eventname-args"><code>emitter[Symbol.for('nodejs.rejection')](err, eventName[, ...args])</code></a></li> 400</ul> 401</li> 402<li><a href="#eventsdefaultmaxlisteners"><code>events.defaultMaxListeners</code></a></li> 403<li><a href="#eventserrormonitor"><code>events.errorMonitor</code></a></li> 404<li><a href="#eventsgeteventlistenersemitterortarget-eventname"><code>events.getEventListeners(emitterOrTarget, eventName)</code></a></li> 405<li><a href="#eventsgetmaxlistenersemitterortarget"><code>events.getMaxListeners(emitterOrTarget)</code></a></li> 406<li><a href="#eventsonceemitter-name-options"><code>events.once(emitter, name[, options])</code></a> 407<ul> 408<li><a href="#awaiting-multiple-events-emitted-on-processnexttick">Awaiting multiple events emitted on <code>process.nextTick()</code></a></li> 409</ul> 410</li> 411<li><a href="#eventscapturerejections"><code>events.captureRejections</code></a></li> 412<li><a href="#eventscapturerejectionsymbol"><code>events.captureRejectionSymbol</code></a></li> 413<li><span class="stability_0"><a href="#eventslistenercountemitter-eventname"><code>events.listenerCount(emitter, eventName)</code></a></span></li> 414<li><a href="#eventsonemitter-eventname-options"><code>events.on(emitter, eventName[, options])</code></a></li> 415<li><a href="#eventssetmaxlistenersn-eventtargets"><code>events.setMaxListeners(n[, ...eventTargets])</code></a></li> 416<li><span class="stability_1"><a href="#eventsaddabortlistenersignal-resource"><code>events.addAbortListener(signal, resource)</code></a></span></li> 417<li><a href="#class-eventseventemitterasyncresource-extends-eventemitter">Class: <code>events.EventEmitterAsyncResource extends EventEmitter</code></a> 418<ul> 419<li><a href="#new-eventseventemitterasyncresourceoptions"><code>new events.EventEmitterAsyncResource([options])</code></a></li> 420<li><a href="#eventemitterasyncresourceasyncid"><code>eventemitterasyncresource.asyncId</code></a></li> 421<li><a href="#eventemitterasyncresourceasyncresource"><code>eventemitterasyncresource.asyncResource</code></a></li> 422<li><a href="#eventemitterasyncresourceemitdestroy"><code>eventemitterasyncresource.emitDestroy()</code></a></li> 423<li><a href="#eventemitterasyncresourcetriggerasyncid"><code>eventemitterasyncresource.triggerAsyncId</code></a></li> 424</ul> 425</li> 426<li><a href="#eventtarget-and-event-api"><code>EventTarget</code> and <code>Event</code> API</a> 427<ul> 428<li><a href="#nodejs-eventtarget-vs-dom-eventtarget">Node.js <code>EventTarget</code> vs. DOM <code>EventTarget</code></a></li> 429<li><a href="#nodeeventtarget-vs-eventemitter"><code>NodeEventTarget</code> vs. <code>EventEmitter</code></a></li> 430<li><a href="#event-listener">Event listener</a></li> 431<li><a href="#eventtarget-error-handling"><code>EventTarget</code> error handling</a></li> 432<li><a href="#class-event">Class: <code>Event</code></a> 433<ul> 434<li><a href="#eventbubbles"><code>event.bubbles</code></a></li> 435<li><span class="stability_3"><a href="#eventcancelbubble"><code>event.cancelBubble</code></a></span></li> 436<li><a href="#eventcancelable"><code>event.cancelable</code></a></li> 437<li><a href="#eventcomposed"><code>event.composed</code></a></li> 438<li><a href="#eventcomposedpath"><code>event.composedPath()</code></a></li> 439<li><a href="#eventcurrenttarget"><code>event.currentTarget</code></a></li> 440<li><a href="#eventdefaultprevented"><code>event.defaultPrevented</code></a></li> 441<li><a href="#eventeventphase"><code>event.eventPhase</code></a></li> 442<li><a href="#eventistrusted"><code>event.isTrusted</code></a></li> 443<li><a href="#eventpreventdefault"><code>event.preventDefault()</code></a></li> 444<li><span class="stability_3"><a href="#eventreturnvalue"><code>event.returnValue</code></a></span></li> 445<li><span class="stability_3"><a href="#eventsrcelement"><code>event.srcElement</code></a></span></li> 446<li><a href="#eventstopimmediatepropagation"><code>event.stopImmediatePropagation()</code></a></li> 447<li><a href="#eventstoppropagation"><code>event.stopPropagation()</code></a></li> 448<li><a href="#eventtarget"><code>event.target</code></a></li> 449<li><a href="#eventtimestamp"><code>event.timeStamp</code></a></li> 450<li><a href="#eventtype"><code>event.type</code></a></li> 451</ul> 452</li> 453<li><a href="#class-eventtarget">Class: <code>EventTarget</code></a> 454<ul> 455<li><a href="#eventtargetaddeventlistenertype-listener-options"><code>eventTarget.addEventListener(type, listener[, options])</code></a></li> 456<li><a href="#eventtargetdispatcheventevent"><code>eventTarget.dispatchEvent(event)</code></a></li> 457<li><a href="#eventtargetremoveeventlistenertype-listener-options"><code>eventTarget.removeEventListener(type, listener[, options])</code></a></li> 458</ul> 459</li> 460<li><span class="stability_1"><a href="#class-customevent">Class: <code>CustomEvent</code></a></span> 461<ul> 462<li><span class="stability_1"><a href="#eventdetail"><code>event.detail</code></a></span></li> 463</ul> 464</li> 465<li><a href="#class-nodeeventtarget">Class: <code>NodeEventTarget</code></a> 466<ul> 467<li><a href="#nodeeventtargetaddlistenertype-listener"><code>nodeEventTarget.addListener(type, listener)</code></a></li> 468<li><a href="#nodeeventtargetemittype-arg"><code>nodeEventTarget.emit(type, arg)</code></a></li> 469<li><a href="#nodeeventtargeteventnames"><code>nodeEventTarget.eventNames()</code></a></li> 470<li><a href="#nodeeventtargetlistenercounttype"><code>nodeEventTarget.listenerCount(type)</code></a></li> 471<li><a href="#nodeeventtargetsetmaxlistenersn"><code>nodeEventTarget.setMaxListeners(n)</code></a></li> 472<li><a href="#nodeeventtargetgetmaxlisteners"><code>nodeEventTarget.getMaxListeners()</code></a></li> 473<li><a href="#nodeeventtargetofftype-listener-options"><code>nodeEventTarget.off(type, listener[, options])</code></a></li> 474<li><a href="#nodeeventtargetontype-listener"><code>nodeEventTarget.on(type, listener)</code></a></li> 475<li><a href="#nodeeventtargetoncetype-listener"><code>nodeEventTarget.once(type, listener)</code></a></li> 476<li><a href="#nodeeventtargetremovealllistenerstype"><code>nodeEventTarget.removeAllListeners([type])</code></a></li> 477<li><a href="#nodeeventtargetremovelistenertype-listener-options"><code>nodeEventTarget.removeListener(type, listener[, options])</code></a></li> 478</ul> 479</li> 480</ul> 481</li> 482</ul> 483</li> 484</ul></details> 485 486 <div id="apicontent"> 487 <h2>Events<span><a class="mark" href="#events" id="events">#</a></span><a aria-hidden="true" class="legacy" id="events_events"></a></h2> 488 489<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 490 491<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.18.2/lib/events.js">lib/events.js</a></p> 492<p>Much of the Node.js core API is built around an idiomatic asynchronous 493event-driven architecture in which certain kinds of objects (called "emitters") 494emit named events that cause <code>Function</code> objects ("listeners") to be called.</p> 495<p>For instance: a <a href="net.html#class-netserver"><code>net.Server</code></a> object emits an event each time a peer 496connects to it; a <a href="fs.html#class-fsreadstream"><code>fs.ReadStream</code></a> emits an event when the file is opened; 497a <a href="stream.html">stream</a> emits an event whenever data is available to be read.</p> 498<p>All objects that emit events are instances of the <code>EventEmitter</code> class. These 499objects expose an <code>eventEmitter.on()</code> function that allows one or more 500functions to be attached to named events emitted by the object. Typically, 501event names are camel-cased strings but any valid JavaScript property key 502can be used.</p> 503<p>When the <code>EventEmitter</code> object emits an event, all of the functions attached 504to that specific event are called <em>synchronously</em>. Any values returned by the 505called listeners are <em>ignored</em> and discarded.</p> 506<p>The following example shows a simple <code>EventEmitter</code> instance with a single 507listener. The <code>eventEmitter.on()</code> method is used to register listeners, while 508the <code>eventEmitter.emit()</code> method is used to trigger the event.</p> 509 510<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 511 512<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> {} 513 514<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 515myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 516 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'an event occurred!'</span>); 517}); 518myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 519 520<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> {} 521 522<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 523myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 524 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'an event occurred!'</span>); 525}); 526myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);</code><button class="copy-button">copy</button></pre> 527<section><h3>Passing arguments and <code>this</code> to listeners<span><a class="mark" href="#passing-arguments-and-this-to-listeners" id="passing-arguments-and-this-to-listeners">#</a></span><a aria-hidden="true" class="legacy" id="events_passing_arguments_and_this_to_listeners"></a></h3> 528<p>The <code>eventEmitter.emit()</code> method allows an arbitrary set of arguments to be 529passed to the listener functions. Keep in mind that when 530an ordinary listener function is called, the standard <code>this</code> keyword 531is intentionally set to reference the <code>EventEmitter</code> instance to which the 532listener is attached.</p> 533 534<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 535<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> {} 536<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 537myEmitter.<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>) { 538 <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); 539 <span class="hljs-comment">// Prints:</span> 540 <span class="hljs-comment">// a b MyEmitter {</span> 541 <span class="hljs-comment">// _events: [Object: null prototype] { event: [Function (anonymous)] },</span> 542 <span class="hljs-comment">// _eventsCount: 1,</span> 543 <span class="hljs-comment">// _maxListeners: undefined,</span> 544 <span class="hljs-comment">// [Symbol(kCapture)]: false</span> 545 <span class="hljs-comment">// } true</span> 546}); 547myEmitter.<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><code class="language-js cjs"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 548<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> {} 549<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 550myEmitter.<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>) { 551 <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); 552 <span class="hljs-comment">// Prints:</span> 553 <span class="hljs-comment">// a b MyEmitter {</span> 554 <span class="hljs-comment">// _events: [Object: null prototype] { event: [Function (anonymous)] },</span> 555 <span class="hljs-comment">// _eventsCount: 1,</span> 556 <span class="hljs-comment">// _maxListeners: undefined,</span> 557 <span class="hljs-comment">// [Symbol(kCapture)]: false</span> 558 <span class="hljs-comment">// } true</span> 559}); 560myEmitter.<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><button class="copy-button">copy</button></pre> 561<p>It is possible to use ES6 Arrow Functions as listeners, however, when doing so, 562the <code>this</code> keyword will no longer reference the <code>EventEmitter</code> instance:</p> 563 564<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 565<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> {} 566<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 567myEmitter.<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> { 568 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(a, b, <span class="hljs-variable language_">this</span>); 569 <span class="hljs-comment">// Prints: a b {}</span> 570}); 571myEmitter.<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><code class="language-js cjs"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 572<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> {} 573<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 574myEmitter.<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> { 575 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(a, b, <span class="hljs-variable language_">this</span>); 576 <span class="hljs-comment">// Prints: a b {}</span> 577}); 578myEmitter.<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><button class="copy-button">copy</button></pre> 579</section><section><h3>Asynchronous vs. synchronous<span><a class="mark" href="#asynchronous-vs-synchronous" id="asynchronous-vs-synchronous">#</a></span><a aria-hidden="true" class="legacy" id="events_asynchronous_vs_synchronous"></a></h3> 580<p>The <code>EventEmitter</code> calls all listeners synchronously in the order in which 581they were registered. This ensures the proper sequencing of 582events and helps avoid race conditions and logic errors. When appropriate, 583listener functions can switch to an asynchronous mode of operation using 584the <code>setImmediate()</code> or <code>process.nextTick()</code> methods:</p> 585 586<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 587<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> {} 588<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 589myEmitter.<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> { 590 <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> { 591 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'this happens asynchronously'</span>); 592 }); 593}); 594myEmitter.<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><code class="language-js cjs"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 595<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> {} 596<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 597myEmitter.<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> { 598 <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> { 599 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'this happens asynchronously'</span>); 600 }); 601}); 602myEmitter.<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><button class="copy-button">copy</button></pre> 603</section><section><h3>Handling events only once<span><a class="mark" href="#handling-events-only-once" id="handling-events-only-once">#</a></span><a aria-hidden="true" class="legacy" id="events_handling_events_only_once"></a></h3> 604<p>When a listener is registered using the <code>eventEmitter.on()</code> method, that 605listener is invoked <em>every time</em> the named event is emitted.</p> 606 607<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 608<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> {} 609<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 610<span class="hljs-keyword">let</span> m = <span class="hljs-number">0</span>; 611myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 612 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(++m); 613}); 614myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 615<span class="hljs-comment">// Prints: 1</span> 616myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 617<span class="hljs-comment">// Prints: 2</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 618<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> {} 619<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 620<span class="hljs-keyword">let</span> m = <span class="hljs-number">0</span>; 621myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 622 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(++m); 623}); 624myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 625<span class="hljs-comment">// Prints: 1</span> 626myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 627<span class="hljs-comment">// Prints: 2</span></code><button class="copy-button">copy</button></pre> 628<p>Using the <code>eventEmitter.once()</code> method, it is possible to register a listener 629that is called at most once for a particular event. Once the event is emitted, 630the listener is unregistered and <em>then</em> called.</p> 631 632<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 633<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> {} 634<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 635<span class="hljs-keyword">let</span> m = <span class="hljs-number">0</span>; 636myEmitter.<span class="hljs-title function_">once</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 637 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(++m); 638}); 639myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 640<span class="hljs-comment">// Prints: 1</span> 641myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 642<span class="hljs-comment">// Ignored</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 643<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> {} 644<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 645<span class="hljs-keyword">let</span> m = <span class="hljs-number">0</span>; 646myEmitter.<span class="hljs-title function_">once</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 647 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(++m); 648}); 649myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 650<span class="hljs-comment">// Prints: 1</span> 651myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 652<span class="hljs-comment">// Ignored</span></code><button class="copy-button">copy</button></pre> 653</section><section><h3>Error events<span><a class="mark" href="#error-events" id="error-events">#</a></span><a aria-hidden="true" class="legacy" id="events_error_events"></a></h3> 654<p>When an error occurs within an <code>EventEmitter</code> instance, the typical action is 655for an <code>'error'</code> event to be emitted. These are treated as special cases 656within Node.js.</p> 657<p>If an <code>EventEmitter</code> does <em>not</em> have at least one listener registered for the 658<code>'error'</code> event, and an <code>'error'</code> event is emitted, the error is thrown, a 659stack trace is printed, and the Node.js process exits.</p> 660 661<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 662<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> {} 663<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 664myEmitter.<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>)); 665<span class="hljs-comment">// Throws and crashes Node.js</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 666<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> {} 667<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 668myEmitter.<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>)); 669<span class="hljs-comment">// Throws and crashes Node.js</span></code><button class="copy-button">copy</button></pre> 670<p>To guard against crashing the Node.js process the <a href="domain.html"><code>domain</code></a> module can be 671used. (Note, however, that the <code>node:domain</code> module is deprecated.)</p> 672<p>As a best practice, listeners should always be added for the <code>'error'</code> events.</p> 673 674<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 675<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> {} 676<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 677myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 678 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'whoops! there was an error'</span>); 679}); 680myEmitter.<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>)); 681<span class="hljs-comment">// Prints: whoops! there was an error</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 682<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> {} 683<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 684myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 685 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'whoops! there was an error'</span>); 686}); 687myEmitter.<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>)); 688<span class="hljs-comment">// Prints: whoops! there was an error</span></code><button class="copy-button">copy</button></pre> 689<p>It is possible to monitor <code>'error'</code> events without consuming the emitted error 690by installing a listener using the symbol <code>events.errorMonitor</code>.</p> 691 692<pre class="with-62-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span>, errorMonitor } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 693 694<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 695myEmitter.<span class="hljs-title function_">on</span>(errorMonitor, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 696 <span class="hljs-title class_">MyMonitoringTool</span>.<span class="hljs-title function_">log</span>(err); 697}); 698myEmitter.<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>)); 699<span class="hljs-comment">// Still throws and crashes Node.js</span></code><code class="language-js cjs"><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">'node:events'</span>); 700 701<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 702myEmitter.<span class="hljs-title function_">on</span>(errorMonitor, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 703 <span class="hljs-title class_">MyMonitoringTool</span>.<span class="hljs-title function_">log</span>(err); 704}); 705myEmitter.<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>)); 706<span class="hljs-comment">// Still throws and crashes Node.js</span></code><button class="copy-button">copy</button></pre> 707</section><section><h3>Capture rejections of promises<span><a class="mark" href="#capture-rejections-of-promises" id="capture-rejections-of-promises">#</a></span><a aria-hidden="true" class="legacy" id="events_capture_rejections_of_promises"></a></h3> 708<p>Using <code>async</code> functions with event handlers is problematic, because it 709can lead to an unhandled rejection in case of a thrown exception:</p> 710 711<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 712<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 713ee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span class="hljs-keyword">async</span> (value) => { 714 <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>); 715});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 716<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 717ee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span class="hljs-keyword">async</span> (value) => { 718 <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>); 719});</code><button class="copy-button">copy</button></pre> 720<p>The <code>captureRejections</code> option in the <code>EventEmitter</code> constructor or the global 721setting change this behavior, installing a <code>.then(undefined, handler)</code> 722handler on the <code>Promise</code>. This handler routes the exception 723asynchronously to the <a href="#emittersymbolfornodejsrejectionerr-eventname-args"><code>Symbol.for('nodejs.rejection')</code></a> method 724if there is one, or to <a href="#error-events"><code>'error'</code></a> event handler if there is none.</p> 725 726<pre class="with-58-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 727<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> }); 728ee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span class="hljs-keyword">async</span> (value) => { 729 <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>); 730}); 731 732ee1.<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>); 733 734<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> }); 735ee2.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span class="hljs-keyword">async</span> (value) => { 736 <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>); 737}); 738 739ee2[<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><code class="language-js cjs"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 740<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> }); 741ee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span class="hljs-keyword">async</span> (value) => { 742 <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>); 743}); 744 745ee1.<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>); 746 747<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> }); 748ee2.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span class="hljs-keyword">async</span> (value) => { 749 <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>); 750}); 751 752ee2[<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><button class="copy-button">copy</button></pre> 753<p>Setting <code>events.captureRejections = true</code> will change the default for all 754new instances of <code>EventEmitter</code>.</p> 755 756<pre class="with-43-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 757 758<span class="hljs-title class_">EventEmitter</span>.<span class="hljs-property">captureRejections</span> = <span class="hljs-literal">true</span>; 759<span class="hljs-keyword">const</span> ee1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 760ee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span class="hljs-keyword">async</span> (value) => { 761 <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>); 762}); 763 764ee1.<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><code class="language-js cjs"><span class="hljs-keyword">const</span> events = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 765events.<span class="hljs-property">captureRejections</span> = <span class="hljs-literal">true</span>; 766<span class="hljs-keyword">const</span> ee1 = <span class="hljs-keyword">new</span> events.<span class="hljs-title class_">EventEmitter</span>(); 767ee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span class="hljs-keyword">async</span> (value) => { 768 <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>); 769}); 770 771ee1.<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><button class="copy-button">copy</button></pre> 772<p>The <code>'error'</code> events that are generated by the <code>captureRejections</code> behavior 773do not have a catch handler to avoid infinite error loops: the 774recommendation is to <strong>not use <code>async</code> functions as <code>'error'</code> event handlers</strong>.</p> 775</section><section><h3>Class: <code>EventEmitter</code><span><a class="mark" href="#class-eventemitter" id="class-eventemitter">#</a></span><a aria-hidden="true" class="legacy" id="events_class_eventemitter"></a></h3> 776<div class="api_metadata"> 777<details class="changelog"><summary>History</summary> 778<table> 779<tbody><tr><th>Version</th><th>Changes</th></tr> 780<tr><td>v13.4.0, v12.16.0</td> 781<td><p>Added captureRejections option.</p></td></tr> 782<tr><td>v0.1.26</td> 783<td><p><span>Added in: v0.1.26</span></p></td></tr> 784</tbody></table> 785</details> 786</div> 787<p>The <code>EventEmitter</code> class is defined and exposed by the <code>node:events</code> module:</p> 788 789<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>);</code><button class="copy-button">copy</button></pre> 790<p>All <code>EventEmitter</code>s emit the event <code>'newListener'</code> when new listeners are 791added and <code>'removeListener'</code> when existing listeners are removed.</p> 792<p>It supports the following option:</p> 793<ul> 794<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 795<a href="#capture-rejections-of-promises">automatic capturing of promise rejection</a>. 796<strong>Default:</strong> <code>false</code>.</li> 797</ul> 798<h4>Event: <code>'newListener'</code><span><a class="mark" href="#event-newlistener" id="event-newlistener">#</a></span><a aria-hidden="true" class="legacy" id="events_event_newlistener"></a></h4> 799<div class="api_metadata"> 800<span>Added in: v0.1.26</span> 801</div> 802<ul> 803<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> 804<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> 805</ul> 806<p>The <code>EventEmitter</code> instance will emit its own <code>'newListener'</code> event <em>before</em> 807a listener is added to its internal array of listeners.</p> 808<p>Listeners registered for the <code>'newListener'</code> event are passed the event 809name and a reference to the listener being added.</p> 810<p>The fact that the event is triggered before adding the listener has a subtle 811but important side effect: any <em>additional</em> listeners registered to the same 812<code>name</code> <em>within</em> the <code>'newListener'</code> callback are inserted <em>before</em> the 813listener that is in the process of being added.</p> 814 815<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 816<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> {} 817 818<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 819<span class="hljs-comment">// Only do this once so we don't loop forever</span> 820myEmitter.<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> { 821 <span class="hljs-keyword">if</span> (event === <span class="hljs-string">'event'</span>) { 822 <span class="hljs-comment">// Insert a new listener in front</span> 823 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 824 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'B'</span>); 825 }); 826 } 827}); 828myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 829 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'A'</span>); 830}); 831myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 832<span class="hljs-comment">// Prints:</span> 833<span class="hljs-comment">// B</span> 834<span class="hljs-comment">// A</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 835<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> {} 836 837<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 838<span class="hljs-comment">// Only do this once so we don't loop forever</span> 839myEmitter.<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> { 840 <span class="hljs-keyword">if</span> (event === <span class="hljs-string">'event'</span>) { 841 <span class="hljs-comment">// Insert a new listener in front</span> 842 myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 843 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'B'</span>); 844 }); 845 } 846}); 847myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 848 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'A'</span>); 849}); 850myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 851<span class="hljs-comment">// Prints:</span> 852<span class="hljs-comment">// B</span> 853<span class="hljs-comment">// A</span></code><button class="copy-button">copy</button></pre> 854<h4>Event: <code>'removeListener'</code><span><a class="mark" href="#event-removelistener" id="event-removelistener">#</a></span><a aria-hidden="true" class="legacy" id="events_event_removelistener"></a></h4> 855<div class="api_metadata"> 856<details class="changelog"><summary>History</summary> 857<table> 858<tbody><tr><th>Version</th><th>Changes</th></tr> 859<tr><td>v6.1.0, v4.7.0</td> 860<td><p>For listeners attached using <code>.once()</code>, the <code>listener</code> argument now yields the original listener function.</p></td></tr> 861<tr><td>v0.9.3</td> 862<td><p><span>Added in: v0.9.3</span></p></td></tr> 863</tbody></table> 864</details> 865</div> 866<ul> 867<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> 868<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> 869</ul> 870<p>The <code>'removeListener'</code> event is emitted <em>after</em> the <code>listener</code> is removed.</p> 871<h4><code>emitter.addListener(eventName, listener)</code><span><a class="mark" href="#emitteraddlistenereventname-listener" id="emitteraddlistenereventname-listener">#</a></span><a aria-hidden="true" class="legacy" id="events_emitter_addlistener_eventname_listener"></a></h4> 872<div class="api_metadata"> 873<span>Added in: v0.1.26</span> 874</div> 875<ul> 876<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> 877<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 878</ul> 879<p>Alias for <code>emitter.on(eventName, listener)</code>.</p> 880<h4><code>emitter.emit(eventName[, ...args])</code><span><a class="mark" href="#emitteremiteventname-args" id="emitteremiteventname-args">#</a></span><a aria-hidden="true" class="legacy" id="events_emitter_emit_eventname_args"></a></h4> 881<div class="api_metadata"> 882<span>Added in: v0.1.26</span> 883</div> 884<ul> 885<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> 886<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 887<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 888</ul> 889<p>Synchronously calls each of the listeners registered for the event named 890<code>eventName</code>, in the order they were registered, passing the supplied arguments 891to each.</p> 892<p>Returns <code>true</code> if the event had listeners, <code>false</code> otherwise.</p> 893 894<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 895<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 896 897<span class="hljs-comment">// First listener</span> 898myEmitter.<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>) { 899 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Helloooo! first listener'</span>); 900}); 901<span class="hljs-comment">// Second listener</span> 902myEmitter.<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>) { 903 <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>); 904}); 905<span class="hljs-comment">// Third listener</span> 906myEmitter.<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>) { 907 <span class="hljs-keyword">const</span> parameters = args.<span class="hljs-title function_">join</span>(<span class="hljs-string">', '</span>); 908 <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>); 909}); 910 911<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>)); 912 913myEmitter.<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>); 914 915<span class="hljs-comment">// Prints:</span> 916<span class="hljs-comment">// [</span> 917<span class="hljs-comment">// [Function: firstListener],</span> 918<span class="hljs-comment">// [Function: secondListener],</span> 919<span class="hljs-comment">// [Function: thirdListener]</span> 920<span class="hljs-comment">// ]</span> 921<span class="hljs-comment">// Helloooo! first listener</span> 922<span class="hljs-comment">// event with parameters 1, 2 in second listener</span> 923<span class="hljs-comment">// event with parameters 1, 2, 3, 4, 5 in third listener</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 924<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 925 926<span class="hljs-comment">// First listener</span> 927myEmitter.<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>) { 928 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Helloooo! first listener'</span>); 929}); 930<span class="hljs-comment">// Second listener</span> 931myEmitter.<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>) { 932 <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>); 933}); 934<span class="hljs-comment">// Third listener</span> 935myEmitter.<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>) { 936 <span class="hljs-keyword">const</span> parameters = args.<span class="hljs-title function_">join</span>(<span class="hljs-string">', '</span>); 937 <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>); 938}); 939 940<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>)); 941 942myEmitter.<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>); 943 944<span class="hljs-comment">// Prints:</span> 945<span class="hljs-comment">// [</span> 946<span class="hljs-comment">// [Function: firstListener],</span> 947<span class="hljs-comment">// [Function: secondListener],</span> 948<span class="hljs-comment">// [Function: thirdListener]</span> 949<span class="hljs-comment">// ]</span> 950<span class="hljs-comment">// Helloooo! first listener</span> 951<span class="hljs-comment">// event with parameters 1, 2 in second listener</span> 952<span class="hljs-comment">// event with parameters 1, 2, 3, 4, 5 in third listener</span></code><button class="copy-button">copy</button></pre> 953<h4><code>emitter.eventNames()</code><span><a class="mark" href="#emittereventnames" id="emittereventnames">#</a></span><a aria-hidden="true" class="legacy" id="events_emitter_eventnames"></a></h4> 954<div class="api_metadata"> 955<span>Added in: v6.0.0</span> 956</div> 957<ul> 958<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a></li> 959</ul> 960<p>Returns an array listing the events for which the emitter has registered 961listeners. The values in the array are strings or <code>Symbol</code>s.</p> 962 963<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 964 965<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 966myEE.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">() =></span> {}); 967myEE.<span class="hljs-title function_">on</span>(<span class="hljs-string">'bar'</span>, <span class="hljs-function">() =></span> {}); 968 969<span class="hljs-keyword">const</span> sym = <span class="hljs-title class_">Symbol</span>(<span class="hljs-string">'symbol'</span>); 970myEE.<span class="hljs-title function_">on</span>(sym, <span class="hljs-function">() =></span> {}); 971 972<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myEE.<span class="hljs-title function_">eventNames</span>()); 973<span class="hljs-comment">// Prints: [ 'foo', 'bar', Symbol(symbol) ]</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 974 975<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 976myEE.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">() =></span> {}); 977myEE.<span class="hljs-title function_">on</span>(<span class="hljs-string">'bar'</span>, <span class="hljs-function">() =></span> {}); 978 979<span class="hljs-keyword">const</span> sym = <span class="hljs-title class_">Symbol</span>(<span class="hljs-string">'symbol'</span>); 980myEE.<span class="hljs-title function_">on</span>(sym, <span class="hljs-function">() =></span> {}); 981 982<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myEE.<span class="hljs-title function_">eventNames</span>()); 983<span class="hljs-comment">// Prints: [ 'foo', 'bar', Symbol(symbol) ]</span></code><button class="copy-button">copy</button></pre> 984<h4><code>emitter.getMaxListeners()</code><span><a class="mark" href="#emittergetmaxlisteners" id="emittergetmaxlisteners">#</a></span><a aria-hidden="true" class="legacy" id="events_emitter_getmaxlisteners"></a></h4> 985<div class="api_metadata"> 986<span>Added in: v1.0.0</span> 987</div> 988<ul> 989<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 990</ul> 991<p>Returns the current max listener value for the <code>EventEmitter</code> which is either 992set by <a href="#emittersetmaxlistenersn"><code>emitter.setMaxListeners(n)</code></a> or defaults to 993<a href="#eventsdefaultmaxlisteners"><code>events.defaultMaxListeners</code></a>.</p> 994<h4><code>emitter.listenerCount(eventName[, listener])</code><span><a class="mark" href="#emitterlistenercounteventname-listener" id="emitterlistenercounteventname-listener">#</a></span><a aria-hidden="true" class="legacy" id="events_emitter_listenercount_eventname_listener"></a></h4> 995<div class="api_metadata"> 996<details class="changelog"><summary>History</summary> 997<table> 998<tbody><tr><th>Version</th><th>Changes</th></tr> 999<tr><td>v18.16.0</td> 1000<td><p>Added the <code>listener</code> argument.</p></td></tr> 1001<tr><td>v3.2.0</td> 1002<td><p><span>Added in: v3.2.0</span></p></td></tr> 1003</tbody></table> 1004</details> 1005</div> 1006<ul> 1007<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> 1008<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> 1009<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1010</ul> 1011<p>Returns the number of listeners listening for the event named <code>eventName</code>. 1012If <code>listener</code> is provided, it will return how many times the listener is found 1013in the list of the listeners of the event.</p> 1014<h4><code>emitter.listeners(eventName)</code><span><a class="mark" href="#emitterlistenerseventname" id="emitterlistenerseventname">#</a></span><a aria-hidden="true" class="legacy" id="events_emitter_listeners_eventname"></a></h4> 1015<div class="api_metadata"> 1016<details class="changelog"><summary>History</summary> 1017<table> 1018<tbody><tr><th>Version</th><th>Changes</th></tr> 1019<tr><td>v7.0.0</td> 1020<td><p>For listeners attached using <code>.once()</code> this returns the original listeners instead of wrapper functions now.</p></td></tr> 1021<tr><td>v0.1.26</td> 1022<td><p><span>Added in: v0.1.26</span></p></td></tr> 1023</tbody></table> 1024</details> 1025</div> 1026<ul> 1027<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> 1028<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function[]></a></li> 1029</ul> 1030<p>Returns a copy of the array of listeners for the event named <code>eventName</code>.</p> 1031<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> { 1032 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'someone connected!'</span>); 1033}); 1034<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>))); 1035<span class="hljs-comment">// Prints: [ [Function] ]</span></code> <button class="copy-button">copy</button></pre> 1036<h4><code>emitter.off(eventName, listener)</code><span><a class="mark" href="#emitteroffeventname-listener" id="emitteroffeventname-listener">#</a></span><a aria-hidden="true" class="legacy" id="events_emitter_off_eventname_listener"></a></h4> 1037<div class="api_metadata"> 1038<span>Added in: v10.0.0</span> 1039</div> 1040<ul> 1041<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> 1042<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1043<li>Returns: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 1044</ul> 1045<p>Alias for <a href="#emitterremovelistenereventname-listener"><code>emitter.removeListener()</code></a>.</p> 1046<h4><code>emitter.on(eventName, listener)</code><span><a class="mark" href="#emitteroneventname-listener" id="emitteroneventname-listener">#</a></span><a aria-hidden="true" class="legacy" id="events_emitter_on_eventname_listener"></a></h4> 1047<div class="api_metadata"> 1048<span>Added in: v0.1.101</span> 1049</div> 1050<ul> 1051<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> 1052<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> 1053<li>Returns: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 1054</ul> 1055<p>Adds the <code>listener</code> function to the end of the listeners array for the 1056event named <code>eventName</code>. No checks are made to see if the <code>listener</code> has 1057already been added. Multiple calls passing the same combination of <code>eventName</code> 1058and <code>listener</code> will result in the <code>listener</code> being added, and called, multiple 1059times.</p> 1060<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> { 1061 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'someone connected!'</span>); 1062});</code> <button class="copy-button">copy</button></pre> 1063<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p> 1064<p>By default, event listeners are invoked in the order they are added. The 1065<code>emitter.prependListener()</code> method can be used as an alternative to add the 1066event listener to the beginning of the listeners array.</p> 1067 1068<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 1069<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1070myEE.<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>)); 1071myEE.<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>)); 1072myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 1073<span class="hljs-comment">// Prints:</span> 1074<span class="hljs-comment">// b</span> 1075<span class="hljs-comment">// a</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 1076<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1077myEE.<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>)); 1078myEE.<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>)); 1079myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 1080<span class="hljs-comment">// Prints:</span> 1081<span class="hljs-comment">// b</span> 1082<span class="hljs-comment">// a</span></code><button class="copy-button">copy</button></pre> 1083<h4><code>emitter.once(eventName, listener)</code><span><a class="mark" href="#emitteronceeventname-listener" id="emitteronceeventname-listener">#</a></span><a aria-hidden="true" class="legacy" id="events_emitter_once_eventname_listener"></a></h4> 1084<div class="api_metadata"> 1085<span>Added in: v0.3.0</span> 1086</div> 1087<ul> 1088<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> 1089<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> 1090<li>Returns: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 1091</ul> 1092<p>Adds a <strong>one-time</strong> <code>listener</code> function for the event named <code>eventName</code>. The 1093next time <code>eventName</code> is triggered, this listener is removed and then invoked.</p> 1094<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> { 1095 <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>); 1096});</code> <button class="copy-button">copy</button></pre> 1097<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p> 1098<p>By default, event listeners are invoked in the order they are added. The 1099<code>emitter.prependOnceListener()</code> method can be used as an alternative to add the 1100event listener to the beginning of the listeners array.</p> 1101 1102<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 1103<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1104myEE.<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>)); 1105myEE.<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>)); 1106myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 1107<span class="hljs-comment">// Prints:</span> 1108<span class="hljs-comment">// b</span> 1109<span class="hljs-comment">// a</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 1110<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1111myEE.<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>)); 1112myEE.<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>)); 1113myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 1114<span class="hljs-comment">// Prints:</span> 1115<span class="hljs-comment">// b</span> 1116<span class="hljs-comment">// a</span></code><button class="copy-button">copy</button></pre> 1117<h4><code>emitter.prependListener(eventName, listener)</code><span><a class="mark" href="#emitterprependlistenereventname-listener" id="emitterprependlistenereventname-listener">#</a></span><a aria-hidden="true" class="legacy" id="events_emitter_prependlistener_eventname_listener"></a></h4> 1118<div class="api_metadata"> 1119<span>Added in: v6.0.0</span> 1120</div> 1121<ul> 1122<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> 1123<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> 1124<li>Returns: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 1125</ul> 1126<p>Adds the <code>listener</code> function to the <em>beginning</em> of the listeners array for the 1127event named <code>eventName</code>. No checks are made to see if the <code>listener</code> has 1128already been added. Multiple calls passing the same combination of <code>eventName</code> 1129and <code>listener</code> will result in the <code>listener</code> being added, and called, multiple 1130times.</p> 1131<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> { 1132 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'someone connected!'</span>); 1133});</code> <button class="copy-button">copy</button></pre> 1134<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p> 1135<h4><code>emitter.prependOnceListener(eventName, listener)</code><span><a class="mark" href="#emitterprependoncelistenereventname-listener" id="emitterprependoncelistenereventname-listener">#</a></span><a aria-hidden="true" class="legacy" id="events_emitter_prependoncelistener_eventname_listener"></a></h4> 1136<div class="api_metadata"> 1137<span>Added in: v6.0.0</span> 1138</div> 1139<ul> 1140<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> 1141<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> 1142<li>Returns: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 1143</ul> 1144<p>Adds a <strong>one-time</strong> <code>listener</code> function for the event named <code>eventName</code> to the 1145<em>beginning</em> of the listeners array. The next time <code>eventName</code> is triggered, this 1146listener is removed, and then invoked.</p> 1147<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> { 1148 <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>); 1149});</code> <button class="copy-button">copy</button></pre> 1150<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p> 1151<h4><code>emitter.removeAllListeners([eventName])</code><span><a class="mark" href="#emitterremovealllistenerseventname" id="emitterremovealllistenerseventname">#</a></span><a aria-hidden="true" class="legacy" id="events_emitter_removealllisteners_eventname"></a></h4> 1152<div class="api_metadata"> 1153<span>Added in: v0.1.26</span> 1154</div> 1155<ul> 1156<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> 1157<li>Returns: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 1158</ul> 1159<p>Removes all listeners, or those of the specified <code>eventName</code>.</p> 1160<p>It is bad practice to remove listeners added elsewhere in the code, 1161particularly when the <code>EventEmitter</code> instance was created by some other 1162component or module (e.g. sockets or file streams).</p> 1163<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p> 1164<h4><code>emitter.removeListener(eventName, listener)</code><span><a class="mark" href="#emitterremovelistenereventname-listener" id="emitterremovelistenereventname-listener">#</a></span><a aria-hidden="true" class="legacy" id="events_emitter_removelistener_eventname_listener"></a></h4> 1165<div class="api_metadata"> 1166<span>Added in: v0.1.26</span> 1167</div> 1168<ul> 1169<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> 1170<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 1171<li>Returns: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 1172</ul> 1173<p>Removes the specified <code>listener</code> from the listener array for the event named 1174<code>eventName</code>.</p> 1175<pre><code class="language-js"><span class="hljs-keyword">const</span> <span class="hljs-title function_">callback</span> = (<span class="hljs-params">stream</span>) => { 1176 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'someone connected!'</span>); 1177}; 1178server.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, callback); 1179<span class="hljs-comment">// ...</span> 1180server.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'connection'</span>, callback);</code> <button class="copy-button">copy</button></pre> 1181<p><code>removeListener()</code> will remove, at most, one instance of a listener from the 1182listener array. If any single listener has been added multiple times to the 1183listener array for the specified <code>eventName</code>, then <code>removeListener()</code> must be 1184called multiple times to remove each instance.</p> 1185<p>Once an event is emitted, all listeners attached to it at the 1186time of emitting are called in order. This implies that any 1187<code>removeListener()</code> or <code>removeAllListeners()</code> calls <em>after</em> emitting and 1188<em>before</em> the last listener finishes execution will not remove them from 1189<code>emit()</code> in progress. Subsequent events behave as expected.</p> 1190 1191<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 1192<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> {} 1193<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 1194 1195<span class="hljs-keyword">const</span> <span class="hljs-title function_">callbackA</span> = (<span class="hljs-params"></span>) => { 1196 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'A'</span>); 1197 myEmitter.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'event'</span>, callbackB); 1198}; 1199 1200<span class="hljs-keyword">const</span> <span class="hljs-title function_">callbackB</span> = (<span class="hljs-params"></span>) => { 1201 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'B'</span>); 1202}; 1203 1204myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, callbackA); 1205 1206myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, callbackB); 1207 1208<span class="hljs-comment">// callbackA removes listener callbackB but it will still be called.</span> 1209<span class="hljs-comment">// Internal listener array at time of emit [callbackA, callbackB]</span> 1210myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 1211<span class="hljs-comment">// Prints:</span> 1212<span class="hljs-comment">// A</span> 1213<span class="hljs-comment">// B</span> 1214 1215<span class="hljs-comment">// callbackB is now removed.</span> 1216<span class="hljs-comment">// Internal listener array [callbackA]</span> 1217myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 1218<span class="hljs-comment">// Prints:</span> 1219<span class="hljs-comment">// A</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 1220<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> {} 1221<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 1222 1223<span class="hljs-keyword">const</span> <span class="hljs-title function_">callbackA</span> = (<span class="hljs-params"></span>) => { 1224 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'A'</span>); 1225 myEmitter.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'event'</span>, callbackB); 1226}; 1227 1228<span class="hljs-keyword">const</span> <span class="hljs-title function_">callbackB</span> = (<span class="hljs-params"></span>) => { 1229 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'B'</span>); 1230}; 1231 1232myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, callbackA); 1233 1234myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, callbackB); 1235 1236<span class="hljs-comment">// callbackA removes listener callbackB but it will still be called.</span> 1237<span class="hljs-comment">// Internal listener array at time of emit [callbackA, callbackB]</span> 1238myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 1239<span class="hljs-comment">// Prints:</span> 1240<span class="hljs-comment">// A</span> 1241<span class="hljs-comment">// B</span> 1242 1243<span class="hljs-comment">// callbackB is now removed.</span> 1244<span class="hljs-comment">// Internal listener array [callbackA]</span> 1245myEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 1246<span class="hljs-comment">// Prints:</span> 1247<span class="hljs-comment">// A</span></code><button class="copy-button">copy</button></pre> 1248<p>Because listeners are managed using an internal array, calling this will 1249change the position indices of any listener registered <em>after</em> the listener 1250being removed. This will not impact the order in which listeners are called, 1251but it means that any copies of the listener array as returned by 1252the <code>emitter.listeners()</code> method will need to be recreated.</p> 1253<p>When a single function has been added as a handler multiple times for a single 1254event (as in the example below), <code>removeListener()</code> will remove the most 1255recently added instance. In the example the <code>once('ping')</code> 1256listener is removed:</p> 1257 1258<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 1259<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1260 1261<span class="hljs-keyword">function</span> <span class="hljs-title function_">pong</span>(<span class="hljs-params"></span>) { 1262 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'pong'</span>); 1263} 1264 1265ee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'ping'</span>, pong); 1266ee.<span class="hljs-title function_">once</span>(<span class="hljs-string">'ping'</span>, pong); 1267ee.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'ping'</span>, pong); 1268 1269ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'ping'</span>); 1270ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'ping'</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 1271<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1272 1273<span class="hljs-keyword">function</span> <span class="hljs-title function_">pong</span>(<span class="hljs-params"></span>) { 1274 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'pong'</span>); 1275} 1276 1277ee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'ping'</span>, pong); 1278ee.<span class="hljs-title function_">once</span>(<span class="hljs-string">'ping'</span>, pong); 1279ee.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'ping'</span>, pong); 1280 1281ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'ping'</span>); 1282ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'ping'</span>);</code><button class="copy-button">copy</button></pre> 1283<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p> 1284<h4><code>emitter.setMaxListeners(n)</code><span><a class="mark" href="#emittersetmaxlistenersn" id="emittersetmaxlistenersn">#</a></span><a aria-hidden="true" class="legacy" id="events_emitter_setmaxlisteners_n"></a></h4> 1285<div class="api_metadata"> 1286<span>Added in: v0.3.5</span> 1287</div> 1288<ul> 1289<li><code>n</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 1290<li>Returns: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 1291</ul> 1292<p>By default <code>EventEmitter</code>s will print a warning if more than <code>10</code> listeners are 1293added for a particular event. This is a useful default that helps finding 1294memory leaks. The <code>emitter.setMaxListeners()</code> method allows the limit to be 1295modified for this specific <code>EventEmitter</code> instance. The value can be set to 1296<code>Infinity</code> (or <code>0</code>) to indicate an unlimited number of listeners.</p> 1297<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p> 1298<h4><code>emitter.rawListeners(eventName)</code><span><a class="mark" href="#emitterrawlistenerseventname" id="emitterrawlistenerseventname">#</a></span><a aria-hidden="true" class="legacy" id="events_emitter_rawlisteners_eventname"></a></h4> 1299<div class="api_metadata"> 1300<span>Added in: v9.4.0</span> 1301</div> 1302<ul> 1303<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> 1304<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function[]></a></li> 1305</ul> 1306<p>Returns a copy of the array of listeners for the event named <code>eventName</code>, 1307including any wrappers (such as those created by <code>.once()</code>).</p> 1308 1309<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 1310<span class="hljs-keyword">const</span> emitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1311emitter.<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>)); 1312 1313<span class="hljs-comment">// Returns a new Array with a function `onceWrapper` which has a property</span> 1314<span class="hljs-comment">// `listener` which contains the original listener bound above</span> 1315<span class="hljs-keyword">const</span> listeners = emitter.<span class="hljs-title function_">rawListeners</span>(<span class="hljs-string">'log'</span>); 1316<span class="hljs-keyword">const</span> logFnWrapper = listeners[<span class="hljs-number">0</span>]; 1317 1318<span class="hljs-comment">// Logs "log once" to the console and does not unbind the `once` event</span> 1319logFnWrapper.<span class="hljs-title function_">listener</span>(); 1320 1321<span class="hljs-comment">// Logs "log once" to the console and removes the listener</span> 1322<span class="hljs-title function_">logFnWrapper</span>(); 1323 1324emitter.<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>)); 1325<span class="hljs-comment">// Will return a new Array with a single function bound by `.on()` above</span> 1326<span class="hljs-keyword">const</span> newListeners = emitter.<span class="hljs-title function_">rawListeners</span>(<span class="hljs-string">'log'</span>); 1327 1328<span class="hljs-comment">// Logs "log persistently" twice</span> 1329newListeners[<span class="hljs-number">0</span>](); 1330emitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'log'</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 1331<span class="hljs-keyword">const</span> emitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1332emitter.<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>)); 1333 1334<span class="hljs-comment">// Returns a new Array with a function `onceWrapper` which has a property</span> 1335<span class="hljs-comment">// `listener` which contains the original listener bound above</span> 1336<span class="hljs-keyword">const</span> listeners = emitter.<span class="hljs-title function_">rawListeners</span>(<span class="hljs-string">'log'</span>); 1337<span class="hljs-keyword">const</span> logFnWrapper = listeners[<span class="hljs-number">0</span>]; 1338 1339<span class="hljs-comment">// Logs "log once" to the console and does not unbind the `once` event</span> 1340logFnWrapper.<span class="hljs-title function_">listener</span>(); 1341 1342<span class="hljs-comment">// Logs "log once" to the console and removes the listener</span> 1343<span class="hljs-title function_">logFnWrapper</span>(); 1344 1345emitter.<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>)); 1346<span class="hljs-comment">// Will return a new Array with a single function bound by `.on()` above</span> 1347<span class="hljs-keyword">const</span> newListeners = emitter.<span class="hljs-title function_">rawListeners</span>(<span class="hljs-string">'log'</span>); 1348 1349<span class="hljs-comment">// Logs "log persistently" twice</span> 1350newListeners[<span class="hljs-number">0</span>](); 1351emitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'log'</span>);</code><button class="copy-button">copy</button></pre> 1352<h4><code>emitter[Symbol.for('nodejs.rejection')](err, eventName[, ...args])</code><span><a class="mark" href="#emittersymbolfornodejsrejectionerr-eventname-args" id="emittersymbolfornodejsrejectionerr-eventname-args">#</a></span><a aria-hidden="true" class="legacy" id="events_emitter_symbol_for_nodejs_rejection_err_eventname_args"></a></h4> 1353<div class="api_metadata"> 1354<details class="changelog"><summary>History</summary> 1355<table> 1356<tbody><tr><th>Version</th><th>Changes</th></tr> 1357<tr><td>v17.4.0, v16.14.0</td> 1358<td><p>No longer experimental.</p></td></tr> 1359<tr><td>v13.4.0, v12.16.0</td> 1360<td><p><span>Added in: v13.4.0, v12.16.0</span></p></td></tr> 1361</tbody></table> 1362</details> 1363</div> 1364<ul> 1365<li><code>err</code> Error</li> 1366<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> 1367<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 1368</ul> 1369<p>The <code>Symbol.for('nodejs.rejection')</code> method is called in case a 1370promise rejection happens when emitting an event and 1371<a href="#capture-rejections-of-promises"><code>captureRejections</code></a> is enabled on the emitter. 1372It is possible to use <a href="#eventscapturerejectionsymbol"><code>events.captureRejectionSymbol</code></a> in 1373place of <code>Symbol.for('nodejs.rejection')</code>.</p> 1374 1375<pre class="with-72-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span>, captureRejectionSymbol } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 1376 1377<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> { 1378 <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) { 1379 <span class="hljs-variable language_">super</span>({ <span class="hljs-attr">captureRejections</span>: <span class="hljs-literal">true</span> }); 1380 } 1381 1382 [captureRejectionSymbol](err, event, ...args) { 1383 <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); 1384 <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">destroy</span>(err); 1385 } 1386 1387 <span class="hljs-title function_">destroy</span>(<span class="hljs-params">err</span>) { 1388 <span class="hljs-comment">// Tear the resource down here.</span> 1389 } 1390}</code><code class="language-js cjs"><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">'node:events'</span>); 1391 1392<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> { 1393 <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) { 1394 <span class="hljs-variable language_">super</span>({ <span class="hljs-attr">captureRejections</span>: <span class="hljs-literal">true</span> }); 1395 } 1396 1397 [captureRejectionSymbol](err, event, ...args) { 1398 <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); 1399 <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">destroy</span>(err); 1400 } 1401 1402 <span class="hljs-title function_">destroy</span>(<span class="hljs-params">err</span>) { 1403 <span class="hljs-comment">// Tear the resource down here.</span> 1404 } 1405}</code><button class="copy-button">copy</button></pre> 1406</section><section><h3><code>events.defaultMaxListeners</code><span><a class="mark" href="#eventsdefaultmaxlisteners" id="eventsdefaultmaxlisteners">#</a></span><a aria-hidden="true" class="legacy" id="events_events_defaultmaxlisteners"></a></h3> 1407<div class="api_metadata"> 1408<span>Added in: v0.11.2</span> 1409</div> 1410<p>By default, a maximum of <code>10</code> listeners can be registered for any single 1411event. This limit can be changed for individual <code>EventEmitter</code> instances 1412using the <a href="#emittersetmaxlistenersn"><code>emitter.setMaxListeners(n)</code></a> method. To change the default 1413for <em>all</em> <code>EventEmitter</code> instances, the <code>events.defaultMaxListeners</code> 1414property can be used. If this value is not a positive number, a <code>RangeError</code> 1415is thrown.</p> 1416<p>Take caution when setting the <code>events.defaultMaxListeners</code> because the 1417change affects <em>all</em> <code>EventEmitter</code> instances, including those created before 1418the change is made. However, calling <a href="#emittersetmaxlistenersn"><code>emitter.setMaxListeners(n)</code></a> still has 1419precedence over <code>events.defaultMaxListeners</code>.</p> 1420<p>This is not a hard limit. The <code>EventEmitter</code> instance will allow 1421more listeners to be added but will output a trace warning to stderr indicating 1422that a "possible EventEmitter memory leak" has been detected. For any single 1423<code>EventEmitter</code>, the <code>emitter.getMaxListeners()</code> and <code>emitter.setMaxListeners()</code> 1424methods can be used to temporarily avoid this warning:</p> 1425 1426<pre class="with-44-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 1427<span class="hljs-keyword">const</span> emitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1428emitter.<span class="hljs-title function_">setMaxListeners</span>(emitter.<span class="hljs-title function_">getMaxListeners</span>() + <span class="hljs-number">1</span>); 1429emitter.<span class="hljs-title function_">once</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 1430 <span class="hljs-comment">// do stuff</span> 1431 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>)); 1432});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> <span class="hljs-title class_">EventEmitter</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 1433<span class="hljs-keyword">const</span> emitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1434emitter.<span class="hljs-title function_">setMaxListeners</span>(emitter.<span class="hljs-title function_">getMaxListeners</span>() + <span class="hljs-number">1</span>); 1435emitter.<span class="hljs-title function_">once</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 1436 <span class="hljs-comment">// do stuff</span> 1437 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>)); 1438});</code><button class="copy-button">copy</button></pre> 1439<p>The <a href="cli.html#--trace-warnings"><code>--trace-warnings</code></a> command-line flag can be used to display the 1440stack trace for such warnings.</p> 1441<p>The emitted warning can be inspected with <a href="process.html#event-warning"><code>process.on('warning')</code></a> and will 1442have the additional <code>emitter</code>, <code>type</code>, and <code>count</code> properties, referring to 1443the event emitter instance, the event's name and the number of attached 1444listeners, respectively. 1445Its <code>name</code> property is set to <code>'MaxListenersExceededWarning'</code>.</p> 1446</section><section><h3><code>events.errorMonitor</code><span><a class="mark" href="#eventserrormonitor" id="eventserrormonitor">#</a></span><a aria-hidden="true" class="legacy" id="events_events_errormonitor"></a></h3> 1447<div class="api_metadata"> 1448<span>Added in: v13.6.0, v12.17.0</span> 1449</div> 1450<p>This symbol shall be used to install a listener for only monitoring <code>'error'</code> 1451events. Listeners installed using this symbol are called before the regular 1452<code>'error'</code> listeners are called.</p> 1453<p>Installing a listener using this symbol does not change the behavior once an 1454<code>'error'</code> event is emitted. Therefore, the process will still crash if no 1455regular <code>'error'</code> listener is installed.</p> 1456</section><section><h3><code>events.getEventListeners(emitterOrTarget, eventName)</code><span><a class="mark" href="#eventsgeteventlistenersemitterortarget-eventname" id="eventsgeteventlistenersemitterortarget-eventname">#</a></span><a aria-hidden="true" class="legacy" id="events_events_geteventlisteners_emitterortarget_eventname"></a></h3> 1457<div class="api_metadata"> 1458<span>Added in: v15.2.0, v14.17.0</span> 1459</div> 1460<ul> 1461<li><code>emitterOrTarget</code> <a href="events.html#class-eventemitter" class="type"><EventEmitter></a> | <a href="events.html#class-eventtarget" class="type"><EventTarget></a></li> 1462<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> 1463<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function[]></a></li> 1464</ul> 1465<p>Returns a copy of the array of listeners for the event named <code>eventName</code>.</p> 1466<p>For <code>EventEmitter</code>s this behaves exactly the same as calling <code>.listeners</code> on 1467the emitter.</p> 1468<p>For <code>EventTarget</code>s this is the only way to get the event listeners for the 1469event target. This is useful for debugging and diagnostic purposes.</p> 1470 1471<pre class="with-67-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { getEventListeners, <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 1472 1473{ 1474 <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1475 <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>); 1476 ee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, listener); 1477 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getEventListeners</span>(ee, <span class="hljs-string">'foo'</span>)); <span class="hljs-comment">// [ [Function: listener] ]</span> 1478} 1479{ 1480 <span class="hljs-keyword">const</span> et = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventTarget</span>(); 1481 <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>); 1482 et.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, listener); 1483 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getEventListeners</span>(et, <span class="hljs-string">'foo'</span>)); <span class="hljs-comment">// [ [Function: listener] ]</span> 1484}</code><code class="language-js cjs"><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">'node:events'</span>); 1485 1486{ 1487 <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1488 <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>); 1489 ee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, listener); 1490 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getEventListeners</span>(ee, <span class="hljs-string">'foo'</span>)); <span class="hljs-comment">// [ [Function: listener] ]</span> 1491} 1492{ 1493 <span class="hljs-keyword">const</span> et = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventTarget</span>(); 1494 <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>); 1495 et.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, listener); 1496 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getEventListeners</span>(et, <span class="hljs-string">'foo'</span>)); <span class="hljs-comment">// [ [Function: listener] ]</span> 1497}</code><button class="copy-button">copy</button></pre> 1498</section><section><h3><code>events.getMaxListeners(emitterOrTarget)</code><span><a class="mark" href="#eventsgetmaxlistenersemitterortarget" id="eventsgetmaxlistenersemitterortarget">#</a></span><a aria-hidden="true" class="legacy" id="events_events_getmaxlisteners_emitterortarget"></a></h3> 1499<div class="api_metadata"> 1500<span>Added in: v18.17.0</span> 1501</div> 1502<ul> 1503<li><code>emitterOrTarget</code> <a href="events.html#class-eventemitter" class="type"><EventEmitter></a> | <a href="events.html#class-eventtarget" class="type"><EventTarget></a></li> 1504<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 1505</ul> 1506<p>Returns the currently set max amount of listeners.</p> 1507<p>For <code>EventEmitter</code>s this behaves exactly the same as calling <code>.getMaxListeners</code> on 1508the emitter.</p> 1509<p>For <code>EventTarget</code>s this is the only way to get the max event listeners for the 1510event target. If the number of event handlers on a single EventTarget exceeds 1511the max set, the EventTarget will print a warning.</p> 1512 1513<pre class="with-82-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { getMaxListeners, setMaxListeners, <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 1514 1515{ 1516 <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1517 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getMaxListeners</span>(ee)); <span class="hljs-comment">// 10</span> 1518 <span class="hljs-title function_">setMaxListeners</span>(<span class="hljs-number">11</span>, ee); 1519 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getMaxListeners</span>(ee)); <span class="hljs-comment">// 11</span> 1520} 1521{ 1522 <span class="hljs-keyword">const</span> et = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventTarget</span>(); 1523 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getMaxListeners</span>(et)); <span class="hljs-comment">// 10</span> 1524 <span class="hljs-title function_">setMaxListeners</span>(<span class="hljs-number">11</span>, et); 1525 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getMaxListeners</span>(et)); <span class="hljs-comment">// 11</span> 1526}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { getMaxListeners, setMaxListeners, <span class="hljs-title class_">EventEmitter</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 1527 1528{ 1529 <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1530 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getMaxListeners</span>(ee)); <span class="hljs-comment">// 10</span> 1531 <span class="hljs-title function_">setMaxListeners</span>(<span class="hljs-number">11</span>, ee); 1532 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getMaxListeners</span>(ee)); <span class="hljs-comment">// 11</span> 1533} 1534{ 1535 <span class="hljs-keyword">const</span> et = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventTarget</span>(); 1536 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getMaxListeners</span>(et)); <span class="hljs-comment">// 10</span> 1537 <span class="hljs-title function_">setMaxListeners</span>(<span class="hljs-number">11</span>, et); 1538 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">getMaxListeners</span>(et)); <span class="hljs-comment">// 11</span> 1539}</code><button class="copy-button">copy</button></pre> 1540</section><section><h3><code>events.once(emitter, name[, options])</code><span><a class="mark" href="#eventsonceemitter-name-options" id="eventsonceemitter-name-options">#</a></span><a aria-hidden="true" class="legacy" id="events_events_once_emitter_name_options"></a></h3> 1541<div class="api_metadata"> 1542<details class="changelog"><summary>History</summary> 1543<table> 1544<tbody><tr><th>Version</th><th>Changes</th></tr> 1545<tr><td>v15.0.0</td> 1546<td><p>The <code>signal</code> option is supported now.</p></td></tr> 1547<tr><td>v11.13.0, v10.16.0</td> 1548<td><p><span>Added in: v11.13.0, v10.16.0</span></p></td></tr> 1549</tbody></table> 1550</details> 1551</div> 1552<ul> 1553<li><code>emitter</code> <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 1554<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 1555<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1556<ul> 1557<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Can be used to cancel waiting for the event.</li> 1558</ul> 1559</li> 1560<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 1561</ul> 1562<p>Creates a <code>Promise</code> that is fulfilled when the <code>EventEmitter</code> emits the given 1563event or that is rejected if the <code>EventEmitter</code> emits <code>'error'</code> while waiting. 1564The <code>Promise</code> will resolve with an array of all the arguments emitted to the 1565given event.</p> 1566<p>This method is intentionally generic and works with the web platform 1567<a href="https://dom.spec.whatwg.org/#interface-eventtarget">EventTarget</a> interface, which has no special 1568<code>'error'</code> event semantics and does not listen to the <code>'error'</code> event.</p> 1569 1570<pre class="with-54-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { once, <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 1571<span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 1572 1573<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1574 1575process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 1576 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'myevent'</span>, <span class="hljs-number">42</span>); 1577}); 1578 1579<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>); 1580<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); 1581 1582<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>); 1583process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 1584 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'error'</span>, err); 1585}); 1586 1587<span class="hljs-keyword">try</span> { 1588 <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(ee, <span class="hljs-string">'myevent'</span>); 1589} <span class="hljs-keyword">catch</span> (err) { 1590 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'error happened'</span>, err); 1591}</code><code class="language-js cjs"><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">'node:events'</span>); 1592 1593<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">run</span>(<span class="hljs-params"></span>) { 1594 <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1595 1596 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 1597 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'myevent'</span>, <span class="hljs-number">42</span>); 1598 }); 1599 1600 <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>); 1601 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); 1602 1603 <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>); 1604 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 1605 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'error'</span>, err); 1606 }); 1607 1608 <span class="hljs-keyword">try</span> { 1609 <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(ee, <span class="hljs-string">'myevent'</span>); 1610 } <span class="hljs-keyword">catch</span> (err) { 1611 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'error happened'</span>, err); 1612 } 1613} 1614 1615<span class="hljs-title function_">run</span>();</code><button class="copy-button">copy</button></pre> 1616<p>The special handling of the <code>'error'</code> event is only used when <code>events.once()</code> 1617is used to wait for another event. If <code>events.once()</code> is used to wait for the 1618'<code>error'</code> event itself, then it is treated as any other kind of event without 1619special handling:</p> 1620 1621<pre class="with-54-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span>, once } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 1622 1623<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1624 1625<span class="hljs-title function_">once</span>(ee, <span class="hljs-string">'error'</span>) 1626 .<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>)) 1627 .<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_">error</span>(<span class="hljs-string">'error'</span>, err.<span class="hljs-property">message</span>)); 1628 1629ee.<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>)); 1630 1631<span class="hljs-comment">// Prints: ok boom</span></code><code class="language-js cjs"><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">'node:events'</span>); 1632 1633<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1634 1635<span class="hljs-title function_">once</span>(ee, <span class="hljs-string">'error'</span>) 1636 .<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>)) 1637 .<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_">error</span>(<span class="hljs-string">'error'</span>, err.<span class="hljs-property">message</span>)); 1638 1639ee.<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>)); 1640 1641<span class="hljs-comment">// Prints: ok boom</span></code><button class="copy-button">copy</button></pre> 1642<p>An <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> can be used to cancel waiting for the event:</p> 1643 1644<pre class="with-54-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span>, once } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 1645 1646<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1647<span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 1648 1649<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>) { 1650 <span class="hljs-keyword">try</span> { 1651 <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(emitter, event, { signal }); 1652 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'event emitted!'</span>); 1653 } <span class="hljs-keyword">catch</span> (error) { 1654 <span class="hljs-keyword">if</span> (error.<span class="hljs-property">name</span> === <span class="hljs-string">'AbortError'</span>) { 1655 <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>); 1656 } <span class="hljs-keyword">else</span> { 1657 <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>); 1658 } 1659 } 1660} 1661 1662<span class="hljs-title function_">foo</span>(ee, <span class="hljs-string">'foo'</span>, ac.<span class="hljs-property">signal</span>); 1663ac.<span class="hljs-title function_">abort</span>(); <span class="hljs-comment">// Abort waiting for the event</span> 1664ee.<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><code class="language-js cjs"><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">'node:events'</span>); 1665 1666<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1667<span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 1668 1669<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>) { 1670 <span class="hljs-keyword">try</span> { 1671 <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(emitter, event, { signal }); 1672 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'event emitted!'</span>); 1673 } <span class="hljs-keyword">catch</span> (error) { 1674 <span class="hljs-keyword">if</span> (error.<span class="hljs-property">name</span> === <span class="hljs-string">'AbortError'</span>) { 1675 <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>); 1676 } <span class="hljs-keyword">else</span> { 1677 <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>); 1678 } 1679 } 1680} 1681 1682<span class="hljs-title function_">foo</span>(ee, <span class="hljs-string">'foo'</span>, ac.<span class="hljs-property">signal</span>); 1683ac.<span class="hljs-title function_">abort</span>(); <span class="hljs-comment">// Abort waiting for the event</span> 1684ee.<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><button class="copy-button">copy</button></pre> 1685<h4>Awaiting multiple events emitted on <code>process.nextTick()</code><span><a class="mark" href="#awaiting-multiple-events-emitted-on-processnexttick" id="awaiting-multiple-events-emitted-on-processnexttick">#</a></span><a aria-hidden="true" class="legacy" id="events_awaiting_multiple_events_emitted_on_process_nexttick"></a></h4> 1686<p>There is an edge case worth noting when using the <code>events.once()</code> function 1687to await multiple events emitted on in the same batch of <code>process.nextTick()</code> 1688operations, or whenever multiple events are emitted synchronously. Specifically, 1689because the <code>process.nextTick()</code> queue is drained before the <code>Promise</code> microtask 1690queue, and because <code>EventEmitter</code> emits all events synchronously, it is possible 1691for <code>events.once()</code> to miss an event.</p> 1692 1693<pre class="with-54-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span>, once } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 1694<span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 1695 1696<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1697 1698<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) { 1699 <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(myEE, <span class="hljs-string">'bar'</span>); 1700 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'bar'</span>); 1701 1702 <span class="hljs-comment">// This Promise will never resolve because the 'foo' event will</span> 1703 <span class="hljs-comment">// have already been emitted before the Promise is created.</span> 1704 <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(myEE, <span class="hljs-string">'foo'</span>); 1705 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'foo'</span>); 1706} 1707 1708process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 1709 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'bar'</span>); 1710 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 1711}); 1712 1713<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><code class="language-js cjs"><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">'node:events'</span>); 1714 1715<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1716 1717<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) { 1718 <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(myEE, <span class="hljs-string">'bar'</span>); 1719 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'bar'</span>); 1720 1721 <span class="hljs-comment">// This Promise will never resolve because the 'foo' event will</span> 1722 <span class="hljs-comment">// have already been emitted before the Promise is created.</span> 1723 <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(myEE, <span class="hljs-string">'foo'</span>); 1724 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'foo'</span>); 1725} 1726 1727process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 1728 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'bar'</span>); 1729 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 1730}); 1731 1732<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><button class="copy-button">copy</button></pre> 1733<p>To catch both events, create each of the Promises <em>before</em> awaiting either 1734of them, then it becomes possible to use <code>Promise.all()</code>, <code>Promise.race()</code>, 1735or <code>Promise.allSettled()</code>:</p> 1736 1737<pre class="with-54-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span>, once } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 1738<span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 1739 1740<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1741 1742<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) { 1743 <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>)]); 1744 <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>); 1745} 1746 1747process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 1748 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'bar'</span>); 1749 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 1750}); 1751 1752<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><code class="language-js cjs"><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">'node:events'</span>); 1753 1754<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1755 1756<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) { 1757 <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>)]); 1758 <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>); 1759} 1760 1761process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 1762 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'bar'</span>); 1763 myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 1764}); 1765 1766<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><button class="copy-button">copy</button></pre> 1767</section><section><h3><code>events.captureRejections</code><span><a class="mark" href="#eventscapturerejections" id="eventscapturerejections">#</a></span><a aria-hidden="true" class="legacy" id="events_events_capturerejections"></a></h3> 1768<div class="api_metadata"> 1769<details class="changelog"><summary>History</summary> 1770<table> 1771<tbody><tr><th>Version</th><th>Changes</th></tr> 1772<tr><td>v17.4.0, v16.14.0</td> 1773<td><p>No longer experimental.</p></td></tr> 1774<tr><td>v13.4.0, v12.16.0</td> 1775<td><p><span>Added in: v13.4.0, v12.16.0</span></p></td></tr> 1776</tbody></table> 1777</details> 1778</div> 1779<p>Value: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></p> 1780<p>Change the default <code>captureRejections</code> option on all new <code>EventEmitter</code> objects.</p> 1781</section><section><h3><code>events.captureRejectionSymbol</code><span><a class="mark" href="#eventscapturerejectionsymbol" id="eventscapturerejectionsymbol">#</a></span><a aria-hidden="true" class="legacy" id="events_events_capturerejectionsymbol"></a></h3> 1782<div class="api_metadata"> 1783<details class="changelog"><summary>History</summary> 1784<table> 1785<tbody><tr><th>Version</th><th>Changes</th></tr> 1786<tr><td>v17.4.0, v16.14.0</td> 1787<td><p>No longer experimental.</p></td></tr> 1788<tr><td>v13.4.0, v12.16.0</td> 1789<td><p><span>Added in: v13.4.0, v12.16.0</span></p></td></tr> 1790</tbody></table> 1791</details> 1792</div> 1793<p>Value: <code>Symbol.for('nodejs.rejection')</code></p> 1794<p>See how to write a custom <a href="#emittersymbolfornodejsrejectionerr-eventname-args">rejection handler</a>.</p> 1795</section><section><h3><code>events.listenerCount(emitter, eventName)</code><span><a class="mark" href="#eventslistenercountemitter-eventname" id="eventslistenercountemitter-eventname">#</a></span><a aria-hidden="true" class="legacy" id="events_events_listenercount_emitter_eventname"></a></h3> 1796<div class="api_metadata"> 1797<span>Added in: v0.9.12</span><span>Deprecated since: v3.2.0</span> 1798</div> 1799<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#emitterlistenercounteventname-listener"><code>emitter.listenerCount()</code></a> instead.</div><p></p> 1800<ul> 1801<li><code>emitter</code> <a href="events.html#class-eventemitter" class="type"><EventEmitter></a> The emitter to query</li> 1802<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> 1803</ul> 1804<p>A class method that returns the number of listeners for the given <code>eventName</code> 1805registered on the given <code>emitter</code>.</p> 1806 1807<pre class="with-63-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitter</span>, listenerCount } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 1808 1809<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1810myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> {}); 1811myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> {}); 1812<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>)); 1813<span class="hljs-comment">// Prints: 2</span></code><code class="language-js cjs"><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">'node:events'</span>); 1814 1815<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1816myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> {}); 1817myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> {}); 1818<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>)); 1819<span class="hljs-comment">// Prints: 2</span></code><button class="copy-button">copy</button></pre> 1820</section><section><h3><code>events.on(emitter, eventName[, options])</code><span><a class="mark" href="#eventsonemitter-eventname-options" id="eventsonemitter-eventname-options">#</a></span><a aria-hidden="true" class="legacy" id="events_events_on_emitter_eventname_options"></a></h3> 1821<div class="api_metadata"> 1822<span>Added in: v13.6.0, v12.16.0</span> 1823</div> 1824<ul> 1825<li><code>emitter</code> <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 1826<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> 1827<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 1828<ul> 1829<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Can be used to cancel awaiting events.</li> 1830</ul> 1831</li> 1832<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> 1833</ul> 1834 1835<pre class="with-52-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { on, <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 1836<span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 1837 1838<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1839 1840<span class="hljs-comment">// Emit later on</span> 1841process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 1842 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'bar'</span>); 1843 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-number">42</span>); 1844}); 1845 1846<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>)) { 1847 <span class="hljs-comment">// The execution of this inner block is synchronous and it</span> 1848 <span class="hljs-comment">// processes one event at a time (even with await). Do not use</span> 1849 <span class="hljs-comment">// if concurrent execution is required.</span> 1850 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event); <span class="hljs-comment">// prints ['bar'] [42]</span> 1851} 1852<span class="hljs-comment">// Unreachable here</span></code><code class="language-js cjs"><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">'node:events'</span>); 1853 1854(<span class="hljs-keyword">async</span> () => { 1855 <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1856 1857 <span class="hljs-comment">// Emit later on</span> 1858 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 1859 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'bar'</span>); 1860 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-number">42</span>); 1861 }); 1862 1863 <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>)) { 1864 <span class="hljs-comment">// The execution of this inner block is synchronous and it</span> 1865 <span class="hljs-comment">// processes one event at a time (even with await). Do not use</span> 1866 <span class="hljs-comment">// if concurrent execution is required.</span> 1867 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event); <span class="hljs-comment">// prints ['bar'] [42]</span> 1868 } 1869 <span class="hljs-comment">// Unreachable here</span> 1870})();</code><button class="copy-button">copy</button></pre> 1871<p>Returns an <code>AsyncIterator</code> that iterates <code>eventName</code> events. It will throw 1872if the <code>EventEmitter</code> emits <code>'error'</code>. It removes all listeners when 1873exiting the loop. The <code>value</code> returned by each iteration is an array 1874composed of the emitted event arguments.</p> 1875<p>An <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> can be used to cancel waiting on events:</p> 1876 1877<pre class="with-52-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { on, <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 1878<span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 1879 1880<span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 1881 1882(<span class="hljs-keyword">async</span> () => { 1883 <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1884 1885 <span class="hljs-comment">// Emit later on</span> 1886 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 1887 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'bar'</span>); 1888 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-number">42</span>); 1889 }); 1890 1891 <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> })) { 1892 <span class="hljs-comment">// The execution of this inner block is synchronous and it</span> 1893 <span class="hljs-comment">// processes one event at a time (even with await). Do not use</span> 1894 <span class="hljs-comment">// if concurrent execution is required.</span> 1895 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event); <span class="hljs-comment">// prints ['bar'] [42]</span> 1896 } 1897 <span class="hljs-comment">// Unreachable here</span> 1898})(); 1899 1900process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> ac.<span class="hljs-title function_">abort</span>());</code><code class="language-js cjs"><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">'node:events'</span>); 1901 1902<span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 1903 1904(<span class="hljs-keyword">async</span> () => { 1905 <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1906 1907 <span class="hljs-comment">// Emit later on</span> 1908 process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 1909 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'bar'</span>); 1910 ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-number">42</span>); 1911 }); 1912 1913 <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> })) { 1914 <span class="hljs-comment">// The execution of this inner block is synchronous and it</span> 1915 <span class="hljs-comment">// processes one event at a time (even with await). Do not use</span> 1916 <span class="hljs-comment">// if concurrent execution is required.</span> 1917 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event); <span class="hljs-comment">// prints ['bar'] [42]</span> 1918 } 1919 <span class="hljs-comment">// Unreachable here</span> 1920})(); 1921 1922process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> ac.<span class="hljs-title function_">abort</span>());</code><button class="copy-button">copy</button></pre> 1923</section><section><h3><code>events.setMaxListeners(n[, ...eventTargets])</code><span><a class="mark" href="#eventssetmaxlistenersn-eventtargets" id="eventssetmaxlistenersn-eventtargets">#</a></span><a aria-hidden="true" class="legacy" id="events_events_setmaxlisteners_n_eventtargets"></a></h3> 1924<div class="api_metadata"> 1925<span>Added in: v15.4.0</span> 1926</div> 1927<ul> 1928<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 1929<code>EventTarget</code> event.</li> 1930<li><code>...eventsTargets</code> <a href="events.html#class-eventtarget" class="type"><EventTarget[]></a> | <a href="events.html#class-eventemitter" class="type"><EventEmitter[]></a> Zero or more <a href="events.html#class-eventtarget" class="type"><EventTarget></a> 1931or <a href="events.html#class-eventemitter" class="type"><EventEmitter></a> instances. If none are specified, <code>n</code> is set as the default 1932max for all newly created <a href="events.html#class-eventtarget" class="type"><EventTarget></a> and <a href="events.html#class-eventemitter" class="type"><EventEmitter></a> objects.</li> 1933</ul> 1934 1935<pre class="with-60-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { setMaxListeners, <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 1936 1937<span class="hljs-keyword">const</span> target = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventTarget</span>(); 1938<span class="hljs-keyword">const</span> emitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1939 1940<span class="hljs-title function_">setMaxListeners</span>(<span class="hljs-number">5</span>, target, emitter);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { 1941 setMaxListeners, 1942 <span class="hljs-title class_">EventEmitter</span>, 1943} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 1944 1945<span class="hljs-keyword">const</span> target = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventTarget</span>(); 1946<span class="hljs-keyword">const</span> emitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 1947 1948<span class="hljs-title function_">setMaxListeners</span>(<span class="hljs-number">5</span>, target, emitter);</code><button class="copy-button">copy</button></pre> 1949</section><section><h3><code>events.addAbortListener(signal, resource)</code><span><a class="mark" href="#eventsaddabortlistenersignal-resource" id="eventsaddabortlistenersignal-resource">#</a></span><a aria-hidden="true" class="legacy" id="events_events_addabortlistener_signal_resource"></a></h3> 1950<div class="api_metadata"> 1951<span>Added in: v18.18.0</span> 1952</div> 1953<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 1954<ul> 1955<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a></li> 1956<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#event-listener" class="type"><EventListener></a></li> 1957<li>Returns: <a href="https://tc39.es/proposal-explicit-resource-management/#sec-disposable-interface" class="type"><Disposable></a> that removes the <code>abort</code> listener.</li> 1958</ul> 1959<p>Listens once to the <code>abort</code> event on the provided <code>signal</code>.</p> 1960<p>Listening to the <code>abort</code> event on abort signals is unsafe and may 1961lead to resource leaks since another third party with the signal can 1962call <a href="#eventstopimmediatepropagation"><code>e.stopImmediatePropagation()</code></a>. Unfortunately Node.js cannot change 1963this since it would violate the web standard. Additionally, the original 1964API makes it easy to forget to remove listeners.</p> 1965<p>This API allows safely using <code>AbortSignal</code>s in Node.js APIs by solving these 1966two issues by listening to the event such that <code>stopImmediatePropagation</code> does 1967not prevent the listener from running.</p> 1968<p>Returns a disposable so that it may be unsubscribed from more easily.</p> 1969 1970<pre class="with-52-chars"><input class="js-flavor-selector" type="checkbox" aria-label="Show modern ES modules syntax"><code class="language-js cjs"><span class="hljs-keyword">const</span> { addAbortListener } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 1971 1972<span class="hljs-keyword">function</span> <span class="hljs-title function_">example</span>(<span class="hljs-params">signal</span>) { 1973 <span class="hljs-keyword">let</span> disposable; 1974 <span class="hljs-keyword">try</span> { 1975 signal.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'abort'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> e.<span class="hljs-title function_">stopImmediatePropagation</span>()); 1976 disposable = <span class="hljs-title function_">addAbortListener</span>(signal, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 1977 <span class="hljs-comment">// Do something when signal is aborted.</span> 1978 }); 1979 } <span class="hljs-keyword">finally</span> { 1980 disposable?.[<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">dispose</span>](); 1981 } 1982}</code><code class="language-js mjs"><span class="hljs-keyword">import</span> { addAbortListener } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 1983 1984<span class="hljs-keyword">function</span> <span class="hljs-title function_">example</span>(<span class="hljs-params">signal</span>) { 1985 <span class="hljs-keyword">let</span> disposable; 1986 <span class="hljs-keyword">try</span> { 1987 signal.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'abort'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> e.<span class="hljs-title function_">stopImmediatePropagation</span>()); 1988 disposable = <span class="hljs-title function_">addAbortListener</span>(signal, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 1989 <span class="hljs-comment">// Do something when signal is aborted.</span> 1990 }); 1991 } <span class="hljs-keyword">finally</span> { 1992 disposable?.[<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">dispose</span>](); 1993 } 1994}</code><button class="copy-button">copy</button></pre> 1995</section><section><h3>Class: <code>events.EventEmitterAsyncResource extends EventEmitter</code><span><a class="mark" href="#class-eventseventemitterasyncresource-extends-eventemitter" id="class-eventseventemitterasyncresource-extends-eventemitter">#</a></span><a aria-hidden="true" class="legacy" id="events_class_events_eventemitterasyncresource_extends_eventemitter"></a></h3> 1996<div class="api_metadata"> 1997<span>Added in: v17.4.0, v16.14.0</span> 1998</div> 1999<p>Integrates <code>EventEmitter</code> with <a href="async_hooks.html#class-asyncresource" class="type"><AsyncResource></a> for <code>EventEmitter</code>s that 2000require manual async tracking. Specifically, all events emitted by instances 2001of <code>events.EventEmitterAsyncResource</code> will run within its <a href="async_context.html">async context</a>.</p> 2002 2003<pre class="with-75-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">EventEmitterAsyncResource</span>, <span class="hljs-title class_">EventEmitter</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:events'</span>; 2004<span class="hljs-keyword">import</span> { notStrictEqual, strictEqual } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>; 2005<span class="hljs-keyword">import</span> { executionAsyncId, triggerAsyncId } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:async_hooks'</span>; 2006 2007<span class="hljs-comment">// Async tracking tooling will identify this as 'Q'.</span> 2008<span class="hljs-keyword">const</span> ee1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitterAsyncResource</span>({ <span class="hljs-attr">name</span>: <span class="hljs-string">'Q'</span> }); 2009 2010<span class="hljs-comment">// 'foo' listeners will run in the EventEmitters async context.</span> 2011ee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">() =></span> { 2012 <span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">executionAsyncId</span>(), ee1.<span class="hljs-property">asyncId</span>); 2013 <span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">triggerAsyncId</span>(), ee1.<span class="hljs-property">triggerAsyncId</span>); 2014}); 2015 2016<span class="hljs-keyword">const</span> ee2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 2017 2018<span class="hljs-comment">// 'foo' listeners on ordinary EventEmitters that do not track async</span> 2019<span class="hljs-comment">// context, however, run in the same async context as the emit().</span> 2020ee2.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">() =></span> { 2021 <span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-title function_">executionAsyncId</span>(), ee2.<span class="hljs-property">asyncId</span>); 2022 <span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-title function_">triggerAsyncId</span>(), ee2.<span class="hljs-property">triggerAsyncId</span>); 2023}); 2024 2025<span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</span>().<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> { 2026 ee1.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 2027 ee2.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 2028});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">EventEmitterAsyncResource</span>, <span class="hljs-title class_">EventEmitter</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 2029<span class="hljs-keyword">const</span> { notStrictEqual, strictEqual } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 2030<span class="hljs-keyword">const</span> { executionAsyncId, triggerAsyncId } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:async_hooks'</span>); 2031 2032<span class="hljs-comment">// Async tracking tooling will identify this as 'Q'.</span> 2033<span class="hljs-keyword">const</span> ee1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitterAsyncResource</span>({ <span class="hljs-attr">name</span>: <span class="hljs-string">'Q'</span> }); 2034 2035<span class="hljs-comment">// 'foo' listeners will run in the EventEmitters async context.</span> 2036ee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">() =></span> { 2037 <span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">executionAsyncId</span>(), ee1.<span class="hljs-property">asyncId</span>); 2038 <span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">triggerAsyncId</span>(), ee1.<span class="hljs-property">triggerAsyncId</span>); 2039}); 2040 2041<span class="hljs-keyword">const</span> ee2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 2042 2043<span class="hljs-comment">// 'foo' listeners on ordinary EventEmitters that do not track async</span> 2044<span class="hljs-comment">// context, however, run in the same async context as the emit().</span> 2045ee2.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">() =></span> { 2046 <span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-title function_">executionAsyncId</span>(), ee2.<span class="hljs-property">asyncId</span>); 2047 <span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-title function_">triggerAsyncId</span>(), ee2.<span class="hljs-property">triggerAsyncId</span>); 2048}); 2049 2050<span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</span>().<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> { 2051 ee1.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 2052 ee2.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 2053});</code><button class="copy-button">copy</button></pre> 2054<p>The <code>EventEmitterAsyncResource</code> class has the same methods and takes the 2055same options as <code>EventEmitter</code> and <code>AsyncResource</code> themselves.</p> 2056<h4><code>new events.EventEmitterAsyncResource([options])</code><span><a class="mark" href="#new-eventseventemitterasyncresourceoptions" id="new-eventseventemitterasyncresourceoptions">#</a></span><a aria-hidden="true" class="legacy" id="events_new_events_eventemitterasyncresource_options"></a></h4> 2057<ul> 2058<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2059<ul> 2060<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 2061<a href="#capture-rejections-of-promises">automatic capturing of promise rejection</a>. 2062<strong>Default:</strong> <code>false</code>.</li> 2063<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The type of async event. <strong>Default:</strong> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new.target"><code>new.target.name</code></a>.</li> 2064<li><code>triggerAsyncId</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The ID of the execution context that created this 2065async event. <strong>Default:</strong> <code>executionAsyncId()</code>.</li> 2066<li><code>requireManualDestroy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If set to <code>true</code>, disables <code>emitDestroy</code> 2067when the object is garbage collected. This usually does not need to be set 2068(even if <code>emitDestroy</code> is called manually), unless the resource's <code>asyncId</code> 2069is retrieved and the sensitive API's <code>emitDestroy</code> is called with it. 2070When set to <code>false</code>, the <code>emitDestroy</code> call on garbage collection 2071will only take place if there is at least one active <code>destroy</code> hook. 2072<strong>Default:</strong> <code>false</code>.</li> 2073</ul> 2074</li> 2075</ul> 2076<h4><code>eventemitterasyncresource.asyncId</code><span><a class="mark" href="#eventemitterasyncresourceasyncid" id="eventemitterasyncresourceasyncid">#</a></span><a aria-hidden="true" class="legacy" id="events_eventemitterasyncresource_asyncid"></a></h4> 2077<ul> 2078<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The unique <code>asyncId</code> assigned to the resource.</li> 2079</ul> 2080<h4><code>eventemitterasyncresource.asyncResource</code><span><a class="mark" href="#eventemitterasyncresourceasyncresource" id="eventemitterasyncresourceasyncresource">#</a></span><a aria-hidden="true" class="legacy" id="events_eventemitterasyncresource_asyncresource"></a></h4> 2081<ul> 2082<li>Type: The underlying <a href="async_hooks.html#class-asyncresource" class="type"><AsyncResource></a>.</li> 2083</ul> 2084<p>The returned <code>AsyncResource</code> object has an additional <code>eventEmitter</code> property 2085that provides a reference to this <code>EventEmitterAsyncResource</code>.</p> 2086<h4><code>eventemitterasyncresource.emitDestroy()</code><span><a class="mark" href="#eventemitterasyncresourceemitdestroy" id="eventemitterasyncresourceemitdestroy">#</a></span><a aria-hidden="true" class="legacy" id="events_eventemitterasyncresource_emitdestroy"></a></h4> 2087<p>Call all <code>destroy</code> hooks. This should only ever be called once. An error will 2088be thrown if it is called more than once. This <strong>must</strong> be manually called. If 2089the resource is left to be collected by the GC then the <code>destroy</code> hooks will 2090never be called.</p> 2091<h4><code>eventemitterasyncresource.triggerAsyncId</code><span><a class="mark" href="#eventemitterasyncresourcetriggerasyncid" id="eventemitterasyncresourcetriggerasyncid">#</a></span><a aria-hidden="true" class="legacy" id="events_eventemitterasyncresource_triggerasyncid"></a></h4> 2092<ul> 2093<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The same <code>triggerAsyncId</code> that is passed to the 2094<code>AsyncResource</code> constructor.</li> 2095</ul> 2096<p><a id="event-target-and-event-api"></a></p> 2097</section><section><h3><code>EventTarget</code> and <code>Event</code> API<span><a class="mark" href="#eventtarget-and-event-api" id="eventtarget-and-event-api">#</a></span><a aria-hidden="true" class="legacy" id="events_eventtarget_and_event_api"></a></h3> 2098<div class="api_metadata"> 2099<details class="changelog"><summary>History</summary> 2100<table> 2101<tbody><tr><th>Version</th><th>Changes</th></tr> 2102<tr><td>v16.0.0</td> 2103<td><p>changed EventTarget error handling.</p></td></tr> 2104<tr><td>v15.4.0</td> 2105<td><p>No longer experimental.</p></td></tr> 2106<tr><td>v15.0.0</td> 2107<td><p>The <code>EventTarget</code> and <code>Event</code> classes are now available as globals.</p></td></tr> 2108<tr><td>v14.5.0</td> 2109<td><p><span>Added in: v14.5.0</span></p></td></tr> 2110</tbody></table> 2111</details> 2112</div> 2113<p>The <code>EventTarget</code> and <code>Event</code> objects are a Node.js-specific implementation 2114of the <a href="https://dom.spec.whatwg.org/#eventtarget"><code>EventTarget</code> Web API</a> that are exposed by some Node.js core APIs.</p> 2115<pre><code class="language-js"><span class="hljs-keyword">const</span> target = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventTarget</span>(); 2116 2117target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">(<span class="hljs-params">event</span>) =></span> { 2118 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'foo event happened!'</span>); 2119});</code> <button class="copy-button">copy</button></pre> 2120<h4>Node.js <code>EventTarget</code> vs. DOM <code>EventTarget</code><span><a class="mark" href="#nodejs-eventtarget-vs-dom-eventtarget" id="nodejs-eventtarget-vs-dom-eventtarget">#</a></span><a aria-hidden="true" class="legacy" id="events_node_js_eventtarget_vs_dom_eventtarget"></a></h4> 2121<p>There are two key differences between the Node.js <code>EventTarget</code> and the 2122<a href="https://dom.spec.whatwg.org/#eventtarget"><code>EventTarget</code> Web API</a>:</p> 2123<ol> 2124<li>Whereas DOM <code>EventTarget</code> instances <em>may</em> be hierarchical, there is no 2125concept of hierarchy and event propagation in Node.js. That is, an event 2126dispatched to an <code>EventTarget</code> does not propagate through a hierarchy of 2127nested target objects that may each have their own set of handlers for the 2128event.</li> 2129<li>In the Node.js <code>EventTarget</code>, if an event listener is an async function 2130or returns a <code>Promise</code>, and the returned <code>Promise</code> rejects, the rejection 2131is automatically captured and handled the same way as a listener that 2132throws synchronously (see <a href="#eventtarget-error-handling"><code>EventTarget</code> error handling</a> for details).</li> 2133</ol> 2134<h4><code>NodeEventTarget</code> vs. <code>EventEmitter</code><span><a class="mark" href="#nodeeventtarget-vs-eventemitter" id="nodeeventtarget-vs-eventemitter">#</a></span><a aria-hidden="true" class="legacy" id="events_nodeeventtarget_vs_eventemitter"></a></h4> 2135<p>The <code>NodeEventTarget</code> object implements a modified subset of the 2136<code>EventEmitter</code> API that allows it to closely <em>emulate</em> an <code>EventEmitter</code> in 2137certain situations. A <code>NodeEventTarget</code> is <em>not</em> an instance of <code>EventEmitter</code> 2138and cannot be used in place of an <code>EventEmitter</code> in most cases.</p> 2139<ol> 2140<li>Unlike <code>EventEmitter</code>, any given <code>listener</code> can be registered at most once 2141per event <code>type</code>. Attempts to register a <code>listener</code> multiple times are 2142ignored.</li> 2143<li>The <code>NodeEventTarget</code> does not emulate the full <code>EventEmitter</code> API. 2144Specifically the <code>prependListener()</code>, <code>prependOnceListener()</code>, 2145<code>rawListeners()</code>, and <code>errorMonitor</code> APIs are not emulated. 2146The <code>'newListener'</code> and <code>'removeListener'</code> events will also not be emitted.</li> 2147<li>The <code>NodeEventTarget</code> does not implement any special default behavior 2148for events with type <code>'error'</code>.</li> 2149<li>The <code>NodeEventTarget</code> supports <code>EventListener</code> objects as well as 2150functions as handlers for all event types.</li> 2151</ol> 2152<h4>Event listener<span><a class="mark" href="#event-listener" id="event-listener">#</a></span><a aria-hidden="true" class="legacy" id="events_event_listener"></a></h4> 2153<p>Event listeners registered for an event <code>type</code> may either be JavaScript 2154functions or objects with a <code>handleEvent</code> property whose value is a function.</p> 2155<p>In either case, the handler function is invoked with the <code>event</code> argument 2156passed to the <code>eventTarget.dispatchEvent()</code> function.</p> 2157<p>Async functions may be used as event listeners. If an async handler function 2158rejects, the rejection is captured and handled as described in 2159<a href="#eventtarget-error-handling"><code>EventTarget</code> error handling</a>.</p> 2160<p>An error thrown by one handler function does not prevent the other handlers 2161from being invoked.</p> 2162<p>The return value of a handler function is ignored.</p> 2163<p>Handlers are always invoked in the order they were added.</p> 2164<p>Handler functions may mutate the <code>event</code> object.</p> 2165<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">handler1</span>(<span class="hljs-params">event</span>) { 2166 <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> 2167 event.<span class="hljs-property">a</span> = <span class="hljs-number">1</span>; 2168} 2169 2170<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>) { 2171 <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> 2172 <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> 2173} 2174 2175<span class="hljs-keyword">const</span> handler3 = { 2176 <span class="hljs-title function_">handleEvent</span>(<span class="hljs-params">event</span>) { 2177 <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> 2178 }, 2179}; 2180 2181<span class="hljs-keyword">const</span> handler4 = { 2182 <span class="hljs-keyword">async</span> <span class="hljs-title function_">handleEvent</span>(<span class="hljs-params">event</span>) { 2183 <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> 2184 }, 2185}; 2186 2187<span class="hljs-keyword">const</span> target = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventTarget</span>(); 2188 2189target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, handler1); 2190target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, handler2); 2191target.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, handler3); 2192target.<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> <button class="copy-button">copy</button></pre> 2193<h4><code>EventTarget</code> error handling<span><a class="mark" href="#eventtarget-error-handling" id="eventtarget-error-handling">#</a></span><a aria-hidden="true" class="legacy" id="events_eventtarget_error_handling"></a></h4> 2194<p>When a registered event listener throws (or returns a Promise that rejects), 2195by default the error is treated as an uncaught exception on 2196<code>process.nextTick()</code>. This means uncaught exceptions in <code>EventTarget</code>s will 2197terminate the Node.js process by default.</p> 2198<p>Throwing within an event listener will <em>not</em> stop the other registered handlers 2199from being invoked.</p> 2200<p>The <code>EventTarget</code> does not implement any special default handling for <code>'error'</code> 2201type events like <code>EventEmitter</code>.</p> 2202<p>Currently errors are first forwarded to the <code>process.on('error')</code> event 2203before reaching <code>process.on('uncaughtException')</code>. This behavior is 2204deprecated and will change in a future release to align <code>EventTarget</code> with 2205other Node.js APIs. Any code relying on the <code>process.on('error')</code> event should 2206be aligned with the new behavior.</p> 2207<h4>Class: <code>Event</code><span><a class="mark" href="#class-event" id="class-event">#</a></span><a aria-hidden="true" class="legacy" id="events_class_event"></a></h4> 2208<div class="api_metadata"> 2209<details class="changelog"><summary>History</summary> 2210<table> 2211<tbody><tr><th>Version</th><th>Changes</th></tr> 2212<tr><td>v15.0.0</td> 2213<td><p>The <code>Event</code> class is now available through the global object.</p></td></tr> 2214<tr><td>v14.5.0</td> 2215<td><p><span>Added in: v14.5.0</span></p></td></tr> 2216</tbody></table> 2217</details> 2218</div> 2219<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 2220are created internally by Node.js.</p> 2221<h5><code>event.bubbles</code><span><a class="mark" href="#eventbubbles" id="eventbubbles">#</a></span><a aria-hidden="true" class="legacy" id="events_event_bubbles"></a></h5> 2222<div class="api_metadata"> 2223<span>Added in: v14.5.0</span> 2224</div> 2225<ul> 2226<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> 2227</ul> 2228<p>This is not used in Node.js and is provided purely for completeness.</p> 2229<h5><code>event.cancelBubble</code><span><a class="mark" href="#eventcancelbubble" id="eventcancelbubble">#</a></span><a aria-hidden="true" class="legacy" id="events_event_cancelbubble"></a></h5> 2230<div class="api_metadata"> 2231<span>Added in: v14.5.0</span> 2232</div> 2233<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use <a href="#eventstoppropagation"><code>event.stopPropagation()</code></a> instead.</div><p></p> 2234<ul> 2235<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2236</ul> 2237<p>Alias for <code>event.stopPropagation()</code> if set to <code>true</code>. This is not used 2238in Node.js and is provided purely for completeness.</p> 2239<h5><code>event.cancelable</code><span><a class="mark" href="#eventcancelable" id="eventcancelable">#</a></span><a aria-hidden="true" class="legacy" id="events_event_cancelable"></a></h5> 2240<div class="api_metadata"> 2241<span>Added in: v14.5.0</span> 2242</div> 2243<ul> 2244<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> 2245</ul> 2246<h5><code>event.composed</code><span><a class="mark" href="#eventcomposed" id="eventcomposed">#</a></span><a aria-hidden="true" class="legacy" id="events_event_composed"></a></h5> 2247<div class="api_metadata"> 2248<span>Added in: v14.5.0</span> 2249</div> 2250<ul> 2251<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> 2252</ul> 2253<p>This is not used in Node.js and is provided purely for completeness.</p> 2254<h5><code>event.composedPath()</code><span><a class="mark" href="#eventcomposedpath" id="eventcomposedpath">#</a></span><a aria-hidden="true" class="legacy" id="events_event_composedpath"></a></h5> 2255<div class="api_metadata"> 2256<span>Added in: v14.5.0</span> 2257</div> 2258<p>Returns an array containing the current <code>EventTarget</code> as the only entry or 2259empty if the event is not being dispatched. This is not used in 2260Node.js and is provided purely for completeness.</p> 2261<h5><code>event.currentTarget</code><span><a class="mark" href="#eventcurrenttarget" id="eventcurrenttarget">#</a></span><a aria-hidden="true" class="legacy" id="events_event_currenttarget"></a></h5> 2262<div class="api_metadata"> 2263<span>Added in: v14.5.0</span> 2264</div> 2265<ul> 2266<li>Type: <a href="events.html#class-eventtarget" class="type"><EventTarget></a> The <code>EventTarget</code> dispatching the event.</li> 2267</ul> 2268<p>Alias for <code>event.target</code>.</p> 2269<h5><code>event.defaultPrevented</code><span><a class="mark" href="#eventdefaultprevented" id="eventdefaultprevented">#</a></span><a aria-hidden="true" class="legacy" id="events_event_defaultprevented"></a></h5> 2270<div class="api_metadata"> 2271<span>Added in: v14.5.0</span> 2272</div> 2273<ul> 2274<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2275</ul> 2276<p>Is <code>true</code> if <code>cancelable</code> is <code>true</code> and <code>event.preventDefault()</code> has been 2277called.</p> 2278<h5><code>event.eventPhase</code><span><a class="mark" href="#eventeventphase" id="eventeventphase">#</a></span><a aria-hidden="true" class="legacy" id="events_event_eventphase"></a></h5> 2279<div class="api_metadata"> 2280<span>Added in: v14.5.0</span> 2281</div> 2282<ul> 2283<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 2284it is being dispatched.</li> 2285</ul> 2286<p>This is not used in Node.js and is provided purely for completeness.</p> 2287<h5><code>event.isTrusted</code><span><a class="mark" href="#eventistrusted" id="eventistrusted">#</a></span><a aria-hidden="true" class="legacy" id="events_event_istrusted"></a></h5> 2288<div class="api_metadata"> 2289<span>Added in: v14.5.0</span> 2290</div> 2291<ul> 2292<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2293</ul> 2294<p>The <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> <code>"abort"</code> event is emitted with <code>isTrusted</code> set to <code>true</code>. The 2295value is <code>false</code> in all other cases.</p> 2296<h5><code>event.preventDefault()</code><span><a class="mark" href="#eventpreventdefault" id="eventpreventdefault">#</a></span><a aria-hidden="true" class="legacy" id="events_event_preventdefault"></a></h5> 2297<div class="api_metadata"> 2298<span>Added in: v14.5.0</span> 2299</div> 2300<p>Sets the <code>defaultPrevented</code> property to <code>true</code> if <code>cancelable</code> is <code>true</code>.</p> 2301<h5><code>event.returnValue</code><span><a class="mark" href="#eventreturnvalue" id="eventreturnvalue">#</a></span><a aria-hidden="true" class="legacy" id="events_event_returnvalue"></a></h5> 2302<div class="api_metadata"> 2303<span>Added in: v14.5.0</span> 2304</div> 2305<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use <a href="#eventdefaultprevented"><code>event.defaultPrevented</code></a> instead.</div><p></p> 2306<ul> 2307<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> 2308</ul> 2309<p>The value of <code>event.returnValue</code> is always the opposite of <code>event.defaultPrevented</code>. 2310This is not used in Node.js and is provided purely for completeness.</p> 2311<h5><code>event.srcElement</code><span><a class="mark" href="#eventsrcelement" id="eventsrcelement">#</a></span><a aria-hidden="true" class="legacy" id="events_event_srcelement"></a></h5> 2312<div class="api_metadata"> 2313<span>Added in: v14.5.0</span> 2314</div> 2315<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use <a href="#eventtarget"><code>event.target</code></a> instead.</div><p></p> 2316<ul> 2317<li>Type: <a href="events.html#class-eventtarget" class="type"><EventTarget></a> The <code>EventTarget</code> dispatching the event.</li> 2318</ul> 2319<p>Alias for <code>event.target</code>.</p> 2320<h5><code>event.stopImmediatePropagation()</code><span><a class="mark" href="#eventstopimmediatepropagation" id="eventstopimmediatepropagation">#</a></span><a aria-hidden="true" class="legacy" id="events_event_stopimmediatepropagation"></a></h5> 2321<div class="api_metadata"> 2322<span>Added in: v14.5.0</span> 2323</div> 2324<p>Stops the invocation of event listeners after the current one completes.</p> 2325<h5><code>event.stopPropagation()</code><span><a class="mark" href="#eventstoppropagation" id="eventstoppropagation">#</a></span><a aria-hidden="true" class="legacy" id="events_event_stoppropagation"></a></h5> 2326<div class="api_metadata"> 2327<span>Added in: v14.5.0</span> 2328</div> 2329<p>This is not used in Node.js and is provided purely for completeness.</p> 2330<h5><code>event.target</code><span><a class="mark" href="#eventtarget" id="eventtarget">#</a></span><a aria-hidden="true" class="legacy" id="events_event_target"></a></h5> 2331<div class="api_metadata"> 2332<span>Added in: v14.5.0</span> 2333</div> 2334<ul> 2335<li>Type: <a href="events.html#class-eventtarget" class="type"><EventTarget></a> The <code>EventTarget</code> dispatching the event.</li> 2336</ul> 2337<h5><code>event.timeStamp</code><span><a class="mark" href="#eventtimestamp" id="eventtimestamp">#</a></span><a aria-hidden="true" class="legacy" id="events_event_timestamp"></a></h5> 2338<div class="api_metadata"> 2339<span>Added in: v14.5.0</span> 2340</div> 2341<ul> 2342<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2343</ul> 2344<p>The millisecond timestamp when the <code>Event</code> object was created.</p> 2345<h5><code>event.type</code><span><a class="mark" href="#eventtype" id="eventtype">#</a></span><a aria-hidden="true" class="legacy" id="events_event_type"></a></h5> 2346<div class="api_metadata"> 2347<span>Added in: v14.5.0</span> 2348</div> 2349<ul> 2350<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2351</ul> 2352<p>The event type identifier.</p> 2353<h4>Class: <code>EventTarget</code><span><a class="mark" href="#class-eventtarget" id="class-eventtarget">#</a></span><a aria-hidden="true" class="legacy" id="events_class_eventtarget"></a></h4> 2354<div class="api_metadata"> 2355<details class="changelog"><summary>History</summary> 2356<table> 2357<tbody><tr><th>Version</th><th>Changes</th></tr> 2358<tr><td>v15.0.0</td> 2359<td><p>The <code>EventTarget</code> class is now available through the global object.</p></td></tr> 2360<tr><td>v14.5.0</td> 2361<td><p><span>Added in: v14.5.0</span></p></td></tr> 2362</tbody></table> 2363</details> 2364</div> 2365<h5><code>eventTarget.addEventListener(type, listener[, options])</code><span><a class="mark" href="#eventtargetaddeventlistenertype-listener-options" id="eventtargetaddeventlistenertype-listener-options">#</a></span><a aria-hidden="true" class="legacy" id="events_eventtarget_addeventlistener_type_listener_options"></a></h5> 2366<div class="api_metadata"> 2367<details class="changelog"><summary>History</summary> 2368<table> 2369<tbody><tr><th>Version</th><th>Changes</th></tr> 2370<tr><td>v15.4.0</td> 2371<td><p>add support for <code>signal</code> option.</p></td></tr> 2372<tr><td>v14.5.0</td> 2373<td><p><span>Added in: v14.5.0</span></p></td></tr> 2374</tbody></table> 2375</details> 2376</div> 2377<ul> 2378<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2379<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#event-listener" class="type"><EventListener></a></li> 2380<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2381<ul> 2382<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 2383when it is first invoked. <strong>Default:</strong> <code>false</code>.</li> 2384<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 2385not call the <code>Event</code> object's <code>preventDefault()</code> method. 2386<strong>Default:</strong> <code>false</code>.</li> 2387<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 2388completeness. <strong>Default:</strong> <code>false</code>.</li> 2389<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> The listener will be removed when the given 2390AbortSignal object's <code>abort()</code> method is called.</li> 2391</ul> 2392</li> 2393</ul> 2394<p>Adds a new handler for the <code>type</code> event. Any given <code>listener</code> is added 2395only once per <code>type</code> and per <code>capture</code> option value.</p> 2396<p>If the <code>once</code> option is <code>true</code>, the <code>listener</code> is removed after the 2397next time a <code>type</code> event is dispatched.</p> 2398<p>The <code>capture</code> option is not used by Node.js in any functional way other than 2399tracking registered event listeners per the <code>EventTarget</code> specification. 2400Specifically, the <code>capture</code> option is used as part of the key when registering 2401a <code>listener</code>. Any individual <code>listener</code> may be added once with 2402<code>capture = false</code>, and once with <code>capture = true</code>.</p> 2403<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">handler</span>(<span class="hljs-params">event</span>) {} 2404 2405<span class="hljs-keyword">const</span> target = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventTarget</span>(); 2406target.<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> 2407target.<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> 2408 2409<span class="hljs-comment">// Removes the second instance of handler</span> 2410target.<span class="hljs-title function_">removeEventListener</span>(<span class="hljs-string">'foo'</span>, handler); 2411 2412<span class="hljs-comment">// Removes the first instance of handler</span> 2413target.<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> <button class="copy-button">copy</button></pre> 2414<h5><code>eventTarget.dispatchEvent(event)</code><span><a class="mark" href="#eventtargetdispatcheventevent" id="eventtargetdispatcheventevent">#</a></span><a aria-hidden="true" class="legacy" id="events_eventtarget_dispatchevent_event"></a></h5> 2415<div class="api_metadata"> 2416<span>Added in: v14.5.0</span> 2417</div> 2418<ul> 2419<li><code>event</code> <a href="events.html#class-event" class="type"><Event></a></li> 2420<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 2421false or its <code>preventDefault()</code> method was not invoked, otherwise <code>false</code>.</li> 2422</ul> 2423<p>Dispatches the <code>event</code> to the list of handlers for <code>event.type</code>.</p> 2424<p>The registered event listeners is synchronously invoked in the order they 2425were registered.</p> 2426<h5><code>eventTarget.removeEventListener(type, listener[, options])</code><span><a class="mark" href="#eventtargetremoveeventlistenertype-listener-options" id="eventtargetremoveeventlistenertype-listener-options">#</a></span><a aria-hidden="true" class="legacy" id="events_eventtarget_removeeventlistener_type_listener_options"></a></h5> 2427<div class="api_metadata"> 2428<span>Added in: v14.5.0</span> 2429</div> 2430<ul> 2431<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2432<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#event-listener" class="type"><EventListener></a></li> 2433<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 2434<ul> 2435<li><code>capture</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2436</ul> 2437</li> 2438</ul> 2439<p>Removes the <code>listener</code> from the list of handlers for event <code>type</code>.</p> 2440<h4>Class: <code>CustomEvent</code><span><a class="mark" href="#class-customevent" id="class-customevent">#</a></span><a aria-hidden="true" class="legacy" id="events_class_customevent"></a></h4> 2441<div class="api_metadata"> 2442<span>Added in: v18.7.0</span> 2443</div> 2444<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental.</div><p></p> 2445<ul> 2446<li>Extends: <a href="events.html#class-event" class="type"><Event></a></li> 2447</ul> 2448<p>The <code>CustomEvent</code> object is an adaptation of the <a href="https://dom.spec.whatwg.org/#customevent"><code>CustomEvent</code> Web API</a>. 2449Instances are created internally by Node.js.</p> 2450<h5><code>event.detail</code><span><a class="mark" href="#eventdetail" id="eventdetail">#</a></span><a aria-hidden="true" class="legacy" id="events_event_detail"></a></h5> 2451<div class="api_metadata"> 2452<span>Added in: v18.7.0</span> 2453</div> 2454<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental.</div><p></p> 2455<ul> 2456<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Returns custom data passed when initializing.</li> 2457</ul> 2458<p>Read-only.</p> 2459<h4>Class: <code>NodeEventTarget</code><span><a class="mark" href="#class-nodeeventtarget" id="class-nodeeventtarget">#</a></span><a aria-hidden="true" class="legacy" id="events_class_nodeeventtarget"></a></h4> 2460<div class="api_metadata"> 2461<span>Added in: v14.5.0</span> 2462</div> 2463<ul> 2464<li>Extends: <a href="events.html#class-eventtarget" class="type"><EventTarget></a></li> 2465</ul> 2466<p>The <code>NodeEventTarget</code> is a Node.js-specific extension to <code>EventTarget</code> 2467that emulates a subset of the <code>EventEmitter</code> API.</p> 2468<h5><code>nodeEventTarget.addListener(type, listener)</code><span><a class="mark" href="#nodeeventtargetaddlistenertype-listener" id="nodeeventtargetaddlistenertype-listener">#</a></span><a aria-hidden="true" class="legacy" id="events_nodeeventtarget_addlistener_type_listener"></a></h5> 2469<div class="api_metadata"> 2470<span>Added in: v14.5.0</span> 2471</div> 2472<ul> 2473<li> 2474<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></p> 2475</li> 2476<li> 2477<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#event-listener" class="type"><EventListener></a></p> 2478</li> 2479<li> 2480<p>Returns: <a href="events.html#class-eventtarget" class="type"><EventTarget></a> this</p> 2481</li> 2482</ul> 2483<p>Node.js-specific extension to the <code>EventTarget</code> class that emulates the 2484equivalent <code>EventEmitter</code> API. The only difference between <code>addListener()</code> and 2485<code>addEventListener()</code> is that <code>addListener()</code> will return a reference to the 2486<code>EventTarget</code>.</p> 2487<h5><code>nodeEventTarget.emit(type, arg)</code><span><a class="mark" href="#nodeeventtargetemittype-arg" id="nodeeventtargetemittype-arg">#</a></span><a aria-hidden="true" class="legacy" id="events_nodeeventtarget_emit_type_arg"></a></h5> 2488<div class="api_metadata"> 2489<span>Added in: v15.2.0</span> 2490</div> 2491<ul> 2492<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 2493<li><code>arg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 2494<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 event listeners registered for the <code>type</code> exist, 2495otherwise <code>false</code>.</li> 2496</ul> 2497<p>Node.js-specific extension to the <code>EventTarget</code> class that dispatches the 2498<code>arg</code> to the list of handlers for <code>type</code>.</p> 2499<h5><code>nodeEventTarget.eventNames()</code><span><a class="mark" href="#nodeeventtargeteventnames" id="nodeeventtargeteventnames">#</a></span><a aria-hidden="true" class="legacy" id="events_nodeeventtarget_eventnames"></a></h5> 2500<div class="api_metadata"> 2501<span>Added in: v14.5.0</span> 2502</div> 2503<ul> 2504<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 2505</ul> 2506<p>Node.js-specific extension to the <code>EventTarget</code> class that returns an array 2507of event <code>type</code> names for which event listeners are registered.</p> 2508<h5><code>nodeEventTarget.listenerCount(type)</code><span><a class="mark" href="#nodeeventtargetlistenercounttype" id="nodeeventtargetlistenercounttype">#</a></span><a aria-hidden="true" class="legacy" id="events_nodeeventtarget_listenercount_type"></a></h5> 2509<div class="api_metadata"> 2510<span>Added in: v14.5.0</span> 2511</div> 2512<ul> 2513<li> 2514<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></p> 2515</li> 2516<li> 2517<p>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></p> 2518</li> 2519</ul> 2520<p>Node.js-specific extension to the <code>EventTarget</code> class that returns the number 2521of event listeners registered for the <code>type</code>.</p> 2522<h5><code>nodeEventTarget.setMaxListeners(n)</code><span><a class="mark" href="#nodeeventtargetsetmaxlistenersn" id="nodeeventtargetsetmaxlistenersn">#</a></span><a aria-hidden="true" class="legacy" id="events_nodeeventtarget_setmaxlisteners_n"></a></h5> 2523<div class="api_metadata"> 2524<span>Added in: v14.5.0</span> 2525</div> 2526<ul> 2527<li><code>n</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2528</ul> 2529<p>Node.js-specific extension to the <code>EventTarget</code> class that sets the number 2530of max event listeners as <code>n</code>.</p> 2531<h5><code>nodeEventTarget.getMaxListeners()</code><span><a class="mark" href="#nodeeventtargetgetmaxlisteners" id="nodeeventtargetgetmaxlisteners">#</a></span><a aria-hidden="true" class="legacy" id="events_nodeeventtarget_getmaxlisteners"></a></h5> 2532<div class="api_metadata"> 2533<span>Added in: v14.5.0</span> 2534</div> 2535<ul> 2536<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 2537</ul> 2538<p>Node.js-specific extension to the <code>EventTarget</code> class that returns the number 2539of max event listeners.</p> 2540<h5><code>nodeEventTarget.off(type, listener[, options])</code><span><a class="mark" href="#nodeeventtargetofftype-listener-options" id="nodeeventtargetofftype-listener-options">#</a></span><a aria-hidden="true" class="legacy" id="events_nodeeventtarget_off_type_listener_options"></a></h5> 2541<div class="api_metadata"> 2542<span>Added in: v14.5.0</span> 2543</div> 2544<ul> 2545<li> 2546<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></p> 2547</li> 2548<li> 2549<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#event-listener" class="type"><EventListener></a></p> 2550</li> 2551<li> 2552<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> 2553<ul> 2554<li><code>capture</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2555</ul> 2556</li> 2557<li> 2558<p>Returns: <a href="events.html#class-eventtarget" class="type"><EventTarget></a> this</p> 2559</li> 2560</ul> 2561<p>Node.js-specific alias for <code>eventTarget.removeEventListener()</code>.</p> 2562<h5><code>nodeEventTarget.on(type, listener)</code><span><a class="mark" href="#nodeeventtargetontype-listener" id="nodeeventtargetontype-listener">#</a></span><a aria-hidden="true" class="legacy" id="events_nodeeventtarget_on_type_listener"></a></h5> 2563<div class="api_metadata"> 2564<span>Added in: v14.5.0</span> 2565</div> 2566<ul> 2567<li> 2568<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></p> 2569</li> 2570<li> 2571<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#event-listener" class="type"><EventListener></a></p> 2572</li> 2573<li> 2574<p>Returns: <a href="events.html#class-eventtarget" class="type"><EventTarget></a> this</p> 2575</li> 2576</ul> 2577<p>Node.js-specific alias for <code>eventTarget.addEventListener()</code>.</p> 2578<h5><code>nodeEventTarget.once(type, listener)</code><span><a class="mark" href="#nodeeventtargetoncetype-listener" id="nodeeventtargetoncetype-listener">#</a></span><a aria-hidden="true" class="legacy" id="events_nodeeventtarget_once_type_listener"></a></h5> 2579<div class="api_metadata"> 2580<span>Added in: v14.5.0</span> 2581</div> 2582<ul> 2583<li> 2584<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></p> 2585</li> 2586<li> 2587<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#event-listener" class="type"><EventListener></a></p> 2588</li> 2589<li> 2590<p>Returns: <a href="events.html#class-eventtarget" class="type"><EventTarget></a> this</p> 2591</li> 2592</ul> 2593<p>Node.js-specific extension to the <code>EventTarget</code> class that adds a <code>once</code> 2594listener for the given event <code>type</code>. This is equivalent to calling <code>on</code> 2595with the <code>once</code> option set to <code>true</code>.</p> 2596<h5><code>nodeEventTarget.removeAllListeners([type])</code><span><a class="mark" href="#nodeeventtargetremovealllistenerstype" id="nodeeventtargetremovealllistenerstype">#</a></span><a aria-hidden="true" class="legacy" id="events_nodeeventtarget_removealllisteners_type"></a></h5> 2597<div class="api_metadata"> 2598<span>Added in: v14.5.0</span> 2599</div> 2600<ul> 2601<li> 2602<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></p> 2603</li> 2604<li> 2605<p>Returns: <a href="events.html#class-eventtarget" class="type"><EventTarget></a> this</p> 2606</li> 2607</ul> 2608<p>Node.js-specific extension to the <code>EventTarget</code> class. If <code>type</code> is specified, 2609removes all registered listeners for <code>type</code>, otherwise removes all registered 2610listeners.</p> 2611<h5><code>nodeEventTarget.removeListener(type, listener[, options])</code><span><a class="mark" href="#nodeeventtargetremovelistenertype-listener-options" id="nodeeventtargetremovelistenertype-listener-options">#</a></span><a aria-hidden="true" class="legacy" id="events_nodeeventtarget_removelistener_type_listener_options"></a></h5> 2612<div class="api_metadata"> 2613<span>Added in: v14.5.0</span> 2614</div> 2615<ul> 2616<li> 2617<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></p> 2618</li> 2619<li> 2620<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#event-listener" class="type"><EventListener></a></p> 2621</li> 2622<li> 2623<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> 2624<ul> 2625<li><code>capture</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 2626</ul> 2627</li> 2628<li> 2629<p>Returns: <a href="events.html#class-eventtarget" class="type"><EventTarget></a> this</p> 2630</li> 2631</ul> 2632<p>Node.js-specific extension to the <code>EventTarget</code> class that removes the 2633<code>listener</code> for the given <code>type</code>. The only difference between <code>removeListener()</code> 2634and <code>removeEventListener()</code> is that <code>removeListener()</code> will return a reference 2635to the <code>EventTarget</code>.</p></section> 2636 <!-- API END --> 2637 </div> 2638 </div> 2639 </div> 2640</body> 2641</html> 2642