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