11cb0ef41Sopenharmony_ci<!DOCTYPE html> 21cb0ef41Sopenharmony_ci<html lang="en"> 31cb0ef41Sopenharmony_ci<head> 41cb0ef41Sopenharmony_ci <meta charset="utf-8"> 51cb0ef41Sopenharmony_ci <meta name="viewport" content="width=device-width"> 61cb0ef41Sopenharmony_ci <meta name="nodejs.org:node-version" content="v18.20.1"> 71cb0ef41Sopenharmony_ci <title>Events | Node.js v18.20.1 Documentation</title> 81cb0ef41Sopenharmony_ci <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 91cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/style.css"> 101cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/hljs.css"> 111cb0ef41Sopenharmony_ci <link rel="canonical" href="https://nodejs.org/api/events.html"> 121cb0ef41Sopenharmony_ci <script async defer src="assets/api.js" type="text/javascript"></script> 131cb0ef41Sopenharmony_ci <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> 141cb0ef41Sopenharmony_ci</head> 151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-events"> 161cb0ef41Sopenharmony_ci <div id="content" class="clearfix"> 171cb0ef41Sopenharmony_ci <div id="column2" class="interior"> 181cb0ef41Sopenharmony_ci <div id="intro" class="interior"> 191cb0ef41Sopenharmony_ci <a href="/" title="Go back to the home page"> 201cb0ef41Sopenharmony_ci Node.js 211cb0ef41Sopenharmony_ci </a> 221cb0ef41Sopenharmony_ci </div> 231cb0ef41Sopenharmony_ci <ul> 241cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 251cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 261cb0ef41Sopenharmony_ci</ul> 271cb0ef41Sopenharmony_ci<hr class="line"> 281cb0ef41Sopenharmony_ci<ul> 291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events active">Events</a></li> 491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 891cb0ef41Sopenharmony_ci</ul> 901cb0ef41Sopenharmony_ci<hr class="line"> 911cb0ef41Sopenharmony_ci<ul> 921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 931cb0ef41Sopenharmony_ci</ul> 941cb0ef41Sopenharmony_ci </div> 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci <div id="column1" data-id="events" class="interior"> 971cb0ef41Sopenharmony_ci <header class="header"> 981cb0ef41Sopenharmony_ci <div class="header-container"> 991cb0ef41Sopenharmony_ci <h1>Node.js v18.20.1 documentation</h1> 1001cb0ef41Sopenharmony_ci <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 1011cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 1021cb0ef41Sopenharmony_ci <path fill="none" d="M0 0h24v24H0z" /> 1031cb0ef41Sopenharmony_ci <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"/> 1041cb0ef41Sopenharmony_ci <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"/> 1051cb0ef41Sopenharmony_ci </svg> 1061cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 1071cb0ef41Sopenharmony_ci <path d="M0 0h24v24H0z" fill="none" /> 1081cb0ef41Sopenharmony_ci <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"/> 1091cb0ef41Sopenharmony_ci </svg> 1101cb0ef41Sopenharmony_ci </button> 1111cb0ef41Sopenharmony_ci </div> 1121cb0ef41Sopenharmony_ci <div id="gtoc"> 1131cb0ef41Sopenharmony_ci <ul> 1141cb0ef41Sopenharmony_ci <li class="pinned-header">Node.js v18.20.1</li> 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci <li class="picker-header"> 1171cb0ef41Sopenharmony_ci <a href="#"> 1181cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 1191cb0ef41Sopenharmony_ci Table of contents 1201cb0ef41Sopenharmony_ci </a> 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci <div class="picker"><div class="toc"><ul> 1231cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#events">Events</a></span> 1241cb0ef41Sopenharmony_ci<ul> 1251cb0ef41Sopenharmony_ci<li><a href="#passing-arguments-and-this-to-listeners">Passing arguments and <code>this</code> to listeners</a></li> 1261cb0ef41Sopenharmony_ci<li><a href="#asynchronous-vs-synchronous">Asynchronous vs. synchronous</a></li> 1271cb0ef41Sopenharmony_ci<li><a href="#handling-events-only-once">Handling events only once</a></li> 1281cb0ef41Sopenharmony_ci<li><a href="#error-events">Error events</a></li> 1291cb0ef41Sopenharmony_ci<li><a href="#capture-rejections-of-promises">Capture rejections of promises</a></li> 1301cb0ef41Sopenharmony_ci<li><a href="#class-eventemitter">Class: <code>EventEmitter</code></a> 1311cb0ef41Sopenharmony_ci<ul> 1321cb0ef41Sopenharmony_ci<li><a href="#event-newlistener">Event: <code>'newListener'</code></a></li> 1331cb0ef41Sopenharmony_ci<li><a href="#event-removelistener">Event: <code>'removeListener'</code></a></li> 1341cb0ef41Sopenharmony_ci<li><a href="#emitteraddlistenereventname-listener"><code>emitter.addListener(eventName, listener)</code></a></li> 1351cb0ef41Sopenharmony_ci<li><a href="#emitteremiteventname-args"><code>emitter.emit(eventName[, ...args])</code></a></li> 1361cb0ef41Sopenharmony_ci<li><a href="#emittereventnames"><code>emitter.eventNames()</code></a></li> 1371cb0ef41Sopenharmony_ci<li><a href="#emittergetmaxlisteners"><code>emitter.getMaxListeners()</code></a></li> 1381cb0ef41Sopenharmony_ci<li><a href="#emitterlistenercounteventname-listener"><code>emitter.listenerCount(eventName[, listener])</code></a></li> 1391cb0ef41Sopenharmony_ci<li><a href="#emitterlistenerseventname"><code>emitter.listeners(eventName)</code></a></li> 1401cb0ef41Sopenharmony_ci<li><a href="#emitteroffeventname-listener"><code>emitter.off(eventName, listener)</code></a></li> 1411cb0ef41Sopenharmony_ci<li><a href="#emitteroneventname-listener"><code>emitter.on(eventName, listener)</code></a></li> 1421cb0ef41Sopenharmony_ci<li><a href="#emitteronceeventname-listener"><code>emitter.once(eventName, listener)</code></a></li> 1431cb0ef41Sopenharmony_ci<li><a href="#emitterprependlistenereventname-listener"><code>emitter.prependListener(eventName, listener)</code></a></li> 1441cb0ef41Sopenharmony_ci<li><a href="#emitterprependoncelistenereventname-listener"><code>emitter.prependOnceListener(eventName, listener)</code></a></li> 1451cb0ef41Sopenharmony_ci<li><a href="#emitterremovealllistenerseventname"><code>emitter.removeAllListeners([eventName])</code></a></li> 1461cb0ef41Sopenharmony_ci<li><a href="#emitterremovelistenereventname-listener"><code>emitter.removeListener(eventName, listener)</code></a></li> 1471cb0ef41Sopenharmony_ci<li><a href="#emittersetmaxlistenersn"><code>emitter.setMaxListeners(n)</code></a></li> 1481cb0ef41Sopenharmony_ci<li><a href="#emitterrawlistenerseventname"><code>emitter.rawListeners(eventName)</code></a></li> 1491cb0ef41Sopenharmony_ci<li><a href="#emittersymbolfornodejsrejectionerr-eventname-args"><code>emitter[Symbol.for('nodejs.rejection')](err, eventName[, ...args])</code></a></li> 1501cb0ef41Sopenharmony_ci</ul> 1511cb0ef41Sopenharmony_ci</li> 1521cb0ef41Sopenharmony_ci<li><a href="#eventsdefaultmaxlisteners"><code>events.defaultMaxListeners</code></a></li> 1531cb0ef41Sopenharmony_ci<li><a href="#eventserrormonitor"><code>events.errorMonitor</code></a></li> 1541cb0ef41Sopenharmony_ci<li><a href="#eventsgeteventlistenersemitterortarget-eventname"><code>events.getEventListeners(emitterOrTarget, eventName)</code></a></li> 1551cb0ef41Sopenharmony_ci<li><a href="#eventsgetmaxlistenersemitterortarget"><code>events.getMaxListeners(emitterOrTarget)</code></a></li> 1561cb0ef41Sopenharmony_ci<li><a href="#eventsonceemitter-name-options"><code>events.once(emitter, name[, options])</code></a> 1571cb0ef41Sopenharmony_ci<ul> 1581cb0ef41Sopenharmony_ci<li><a href="#awaiting-multiple-events-emitted-on-processnexttick">Awaiting multiple events emitted on <code>process.nextTick()</code></a></li> 1591cb0ef41Sopenharmony_ci</ul> 1601cb0ef41Sopenharmony_ci</li> 1611cb0ef41Sopenharmony_ci<li><a href="#eventscapturerejections"><code>events.captureRejections</code></a></li> 1621cb0ef41Sopenharmony_ci<li><a href="#eventscapturerejectionsymbol"><code>events.captureRejectionSymbol</code></a></li> 1631cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#eventslistenercountemitter-eventname"><code>events.listenerCount(emitter, eventName)</code></a></span></li> 1641cb0ef41Sopenharmony_ci<li><a href="#eventsonemitter-eventname-options"><code>events.on(emitter, eventName[, options])</code></a></li> 1651cb0ef41Sopenharmony_ci<li><a href="#eventssetmaxlistenersn-eventtargets"><code>events.setMaxListeners(n[, ...eventTargets])</code></a></li> 1661cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#eventsaddabortlistenersignal-resource"><code>events.addAbortListener(signal, resource)</code></a></span></li> 1671cb0ef41Sopenharmony_ci<li><a href="#class-eventseventemitterasyncresource-extends-eventemitter">Class: <code>events.EventEmitterAsyncResource extends EventEmitter</code></a> 1681cb0ef41Sopenharmony_ci<ul> 1691cb0ef41Sopenharmony_ci<li><a href="#new-eventseventemitterasyncresourceoptions"><code>new events.EventEmitterAsyncResource([options])</code></a></li> 1701cb0ef41Sopenharmony_ci<li><a href="#eventemitterasyncresourceasyncid"><code>eventemitterasyncresource.asyncId</code></a></li> 1711cb0ef41Sopenharmony_ci<li><a href="#eventemitterasyncresourceasyncresource"><code>eventemitterasyncresource.asyncResource</code></a></li> 1721cb0ef41Sopenharmony_ci<li><a href="#eventemitterasyncresourceemitdestroy"><code>eventemitterasyncresource.emitDestroy()</code></a></li> 1731cb0ef41Sopenharmony_ci<li><a href="#eventemitterasyncresourcetriggerasyncid"><code>eventemitterasyncresource.triggerAsyncId</code></a></li> 1741cb0ef41Sopenharmony_ci</ul> 1751cb0ef41Sopenharmony_ci</li> 1761cb0ef41Sopenharmony_ci<li><a href="#eventtarget-and-event-api"><code>EventTarget</code> and <code>Event</code> API</a> 1771cb0ef41Sopenharmony_ci<ul> 1781cb0ef41Sopenharmony_ci<li><a href="#nodejs-eventtarget-vs-dom-eventtarget">Node.js <code>EventTarget</code> vs. DOM <code>EventTarget</code></a></li> 1791cb0ef41Sopenharmony_ci<li><a href="#nodeeventtarget-vs-eventemitter"><code>NodeEventTarget</code> vs. <code>EventEmitter</code></a></li> 1801cb0ef41Sopenharmony_ci<li><a href="#event-listener">Event listener</a></li> 1811cb0ef41Sopenharmony_ci<li><a href="#eventtarget-error-handling"><code>EventTarget</code> error handling</a></li> 1821cb0ef41Sopenharmony_ci<li><a href="#class-event">Class: <code>Event</code></a> 1831cb0ef41Sopenharmony_ci<ul> 1841cb0ef41Sopenharmony_ci<li><a href="#eventbubbles"><code>event.bubbles</code></a></li> 1851cb0ef41Sopenharmony_ci<li><span class="stability_3"><a href="#eventcancelbubble"><code>event.cancelBubble</code></a></span></li> 1861cb0ef41Sopenharmony_ci<li><a href="#eventcancelable"><code>event.cancelable</code></a></li> 1871cb0ef41Sopenharmony_ci<li><a href="#eventcomposed"><code>event.composed</code></a></li> 1881cb0ef41Sopenharmony_ci<li><a href="#eventcomposedpath"><code>event.composedPath()</code></a></li> 1891cb0ef41Sopenharmony_ci<li><a href="#eventcurrenttarget"><code>event.currentTarget</code></a></li> 1901cb0ef41Sopenharmony_ci<li><a href="#eventdefaultprevented"><code>event.defaultPrevented</code></a></li> 1911cb0ef41Sopenharmony_ci<li><a href="#eventeventphase"><code>event.eventPhase</code></a></li> 1921cb0ef41Sopenharmony_ci<li><a href="#eventistrusted"><code>event.isTrusted</code></a></li> 1931cb0ef41Sopenharmony_ci<li><a href="#eventpreventdefault"><code>event.preventDefault()</code></a></li> 1941cb0ef41Sopenharmony_ci<li><span class="stability_3"><a href="#eventreturnvalue"><code>event.returnValue</code></a></span></li> 1951cb0ef41Sopenharmony_ci<li><span class="stability_3"><a href="#eventsrcelement"><code>event.srcElement</code></a></span></li> 1961cb0ef41Sopenharmony_ci<li><a href="#eventstopimmediatepropagation"><code>event.stopImmediatePropagation()</code></a></li> 1971cb0ef41Sopenharmony_ci<li><a href="#eventstoppropagation"><code>event.stopPropagation()</code></a></li> 1981cb0ef41Sopenharmony_ci<li><a href="#eventtarget"><code>event.target</code></a></li> 1991cb0ef41Sopenharmony_ci<li><a href="#eventtimestamp"><code>event.timeStamp</code></a></li> 2001cb0ef41Sopenharmony_ci<li><a href="#eventtype"><code>event.type</code></a></li> 2011cb0ef41Sopenharmony_ci</ul> 2021cb0ef41Sopenharmony_ci</li> 2031cb0ef41Sopenharmony_ci<li><a href="#class-eventtarget">Class: <code>EventTarget</code></a> 2041cb0ef41Sopenharmony_ci<ul> 2051cb0ef41Sopenharmony_ci<li><a href="#eventtargetaddeventlistenertype-listener-options"><code>eventTarget.addEventListener(type, listener[, options])</code></a></li> 2061cb0ef41Sopenharmony_ci<li><a href="#eventtargetdispatcheventevent"><code>eventTarget.dispatchEvent(event)</code></a></li> 2071cb0ef41Sopenharmony_ci<li><a href="#eventtargetremoveeventlistenertype-listener-options"><code>eventTarget.removeEventListener(type, listener[, options])</code></a></li> 2081cb0ef41Sopenharmony_ci</ul> 2091cb0ef41Sopenharmony_ci</li> 2101cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#class-customevent">Class: <code>CustomEvent</code></a></span> 2111cb0ef41Sopenharmony_ci<ul> 2121cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#eventdetail"><code>event.detail</code></a></span></li> 2131cb0ef41Sopenharmony_ci</ul> 2141cb0ef41Sopenharmony_ci</li> 2151cb0ef41Sopenharmony_ci<li><a href="#class-nodeeventtarget">Class: <code>NodeEventTarget</code></a> 2161cb0ef41Sopenharmony_ci<ul> 2171cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargetaddlistenertype-listener"><code>nodeEventTarget.addListener(type, listener)</code></a></li> 2181cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargetemittype-arg"><code>nodeEventTarget.emit(type, arg)</code></a></li> 2191cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargeteventnames"><code>nodeEventTarget.eventNames()</code></a></li> 2201cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargetlistenercounttype"><code>nodeEventTarget.listenerCount(type)</code></a></li> 2211cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargetsetmaxlistenersn"><code>nodeEventTarget.setMaxListeners(n)</code></a></li> 2221cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargetgetmaxlisteners"><code>nodeEventTarget.getMaxListeners()</code></a></li> 2231cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargetofftype-listener-options"><code>nodeEventTarget.off(type, listener[, options])</code></a></li> 2241cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargetontype-listener"><code>nodeEventTarget.on(type, listener)</code></a></li> 2251cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargetoncetype-listener"><code>nodeEventTarget.once(type, listener)</code></a></li> 2261cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargetremovealllistenerstype"><code>nodeEventTarget.removeAllListeners([type])</code></a></li> 2271cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargetremovelistenertype-listener-options"><code>nodeEventTarget.removeListener(type, listener[, options])</code></a></li> 2281cb0ef41Sopenharmony_ci</ul> 2291cb0ef41Sopenharmony_ci</li> 2301cb0ef41Sopenharmony_ci</ul> 2311cb0ef41Sopenharmony_ci</li> 2321cb0ef41Sopenharmony_ci</ul> 2331cb0ef41Sopenharmony_ci</li> 2341cb0ef41Sopenharmony_ci</ul></div></div> 2351cb0ef41Sopenharmony_ci </li> 2361cb0ef41Sopenharmony_ci 2371cb0ef41Sopenharmony_ci 2381cb0ef41Sopenharmony_ci <li class="picker-header"> 2391cb0ef41Sopenharmony_ci <a href="#"> 2401cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 2411cb0ef41Sopenharmony_ci Index 2421cb0ef41Sopenharmony_ci </a> 2431cb0ef41Sopenharmony_ci 2441cb0ef41Sopenharmony_ci <div class="picker"><ul> 2451cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 2461cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 2471cb0ef41Sopenharmony_ci 2481cb0ef41Sopenharmony_ci <li> 2491cb0ef41Sopenharmony_ci <a href="index.html">Index</a> 2501cb0ef41Sopenharmony_ci </li> 2511cb0ef41Sopenharmony_ci </ul> 2521cb0ef41Sopenharmony_ci 2531cb0ef41Sopenharmony_ci<hr class="line"> 2541cb0ef41Sopenharmony_ci<ul> 2551cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 2561cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 2571cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 2581cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 2591cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 2601cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 2611cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 2621cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 2631cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 2641cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 2651cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 2661cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 2671cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 2681cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 2691cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 2701cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 2711cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 2721cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 2731cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 2741cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events active">Events</a></li> 2751cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 2761cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 2771cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 2781cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 2791cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 2801cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 2811cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 2821cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 2831cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 2841cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 2851cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 2861cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 2871cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 2881cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 2891cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 2901cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 2911cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 2921cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 2931cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 2941cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 2951cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 2961cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 2971cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 2981cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 2991cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 3001cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 3011cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 3021cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 3031cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 3041cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 3051cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 3061cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 3071cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 3081cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 3091cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 3101cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 3111cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 3121cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 3131cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 3141cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 3151cb0ef41Sopenharmony_ci</ul> 3161cb0ef41Sopenharmony_ci<hr class="line"> 3171cb0ef41Sopenharmony_ci<ul> 3181cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 3191cb0ef41Sopenharmony_ci</ul></div> 3201cb0ef41Sopenharmony_ci </li> 3211cb0ef41Sopenharmony_ci 3221cb0ef41Sopenharmony_ci 3231cb0ef41Sopenharmony_ci <li class="picker-header"> 3241cb0ef41Sopenharmony_ci <a href="#"> 3251cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 3261cb0ef41Sopenharmony_ci Other versions 3271cb0ef41Sopenharmony_ci </a> 3281cb0ef41Sopenharmony_ci <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/events.html">21.x</a></li> 3291cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/events.html">20.x <b>LTS</b></a></li> 3301cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/events.html">19.x</a></li> 3311cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/events.html">18.x <b>LTS</b></a></li> 3321cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v17.x/api/events.html">17.x</a></li> 3331cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v16.x/api/events.html">16.x</a></li> 3341cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v15.x/api/events.html">15.x</a></li> 3351cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v14.x/api/events.html">14.x</a></li> 3361cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v13.x/api/events.html">13.x</a></li> 3371cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v12.x/api/events.html">12.x</a></li> 3381cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v11.x/api/events.html">11.x</a></li> 3391cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v10.x/api/events.html">10.x</a></li> 3401cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v9.x/api/events.html">9.x</a></li> 3411cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v8.x/api/events.html">8.x</a></li> 3421cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v7.x/api/events.html">7.x</a></li> 3431cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v6.x/api/events.html">6.x</a></li> 3441cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v5.x/api/events.html">5.x</a></li> 3451cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v4.x/api/events.html">4.x</a></li> 3461cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/events.html">0.12.x</a></li> 3471cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/events.html">0.10.x</a></li></ol></div> 3481cb0ef41Sopenharmony_ci </li> 3491cb0ef41Sopenharmony_ci 3501cb0ef41Sopenharmony_ci <li class="picker-header"> 3511cb0ef41Sopenharmony_ci <a href="#"> 3521cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 3531cb0ef41Sopenharmony_ci Options 3541cb0ef41Sopenharmony_ci </a> 3551cb0ef41Sopenharmony_ci 3561cb0ef41Sopenharmony_ci <div class="picker"> 3571cb0ef41Sopenharmony_ci <ul> 3581cb0ef41Sopenharmony_ci <li> 3591cb0ef41Sopenharmony_ci <a href="all.html">View on single page</a> 3601cb0ef41Sopenharmony_ci </li> 3611cb0ef41Sopenharmony_ci <li> 3621cb0ef41Sopenharmony_ci <a href="events.json">View as JSON</a> 3631cb0ef41Sopenharmony_ci </li> 3641cb0ef41Sopenharmony_ci <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/events.md">Edit on GitHub</a></li> 3651cb0ef41Sopenharmony_ci </ul> 3661cb0ef41Sopenharmony_ci </div> 3671cb0ef41Sopenharmony_ci </li> 3681cb0ef41Sopenharmony_ci </ul> 3691cb0ef41Sopenharmony_ci </div> 3701cb0ef41Sopenharmony_ci <hr> 3711cb0ef41Sopenharmony_ci </header> 3721cb0ef41Sopenharmony_ci 3731cb0ef41Sopenharmony_ci <details id="toc" open><summary>Table of contents</summary><ul> 3741cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#events">Events</a></span> 3751cb0ef41Sopenharmony_ci<ul> 3761cb0ef41Sopenharmony_ci<li><a href="#passing-arguments-and-this-to-listeners">Passing arguments and <code>this</code> to listeners</a></li> 3771cb0ef41Sopenharmony_ci<li><a href="#asynchronous-vs-synchronous">Asynchronous vs. synchronous</a></li> 3781cb0ef41Sopenharmony_ci<li><a href="#handling-events-only-once">Handling events only once</a></li> 3791cb0ef41Sopenharmony_ci<li><a href="#error-events">Error events</a></li> 3801cb0ef41Sopenharmony_ci<li><a href="#capture-rejections-of-promises">Capture rejections of promises</a></li> 3811cb0ef41Sopenharmony_ci<li><a href="#class-eventemitter">Class: <code>EventEmitter</code></a> 3821cb0ef41Sopenharmony_ci<ul> 3831cb0ef41Sopenharmony_ci<li><a href="#event-newlistener">Event: <code>'newListener'</code></a></li> 3841cb0ef41Sopenharmony_ci<li><a href="#event-removelistener">Event: <code>'removeListener'</code></a></li> 3851cb0ef41Sopenharmony_ci<li><a href="#emitteraddlistenereventname-listener"><code>emitter.addListener(eventName, listener)</code></a></li> 3861cb0ef41Sopenharmony_ci<li><a href="#emitteremiteventname-args"><code>emitter.emit(eventName[, ...args])</code></a></li> 3871cb0ef41Sopenharmony_ci<li><a href="#emittereventnames"><code>emitter.eventNames()</code></a></li> 3881cb0ef41Sopenharmony_ci<li><a href="#emittergetmaxlisteners"><code>emitter.getMaxListeners()</code></a></li> 3891cb0ef41Sopenharmony_ci<li><a href="#emitterlistenercounteventname-listener"><code>emitter.listenerCount(eventName[, listener])</code></a></li> 3901cb0ef41Sopenharmony_ci<li><a href="#emitterlistenerseventname"><code>emitter.listeners(eventName)</code></a></li> 3911cb0ef41Sopenharmony_ci<li><a href="#emitteroffeventname-listener"><code>emitter.off(eventName, listener)</code></a></li> 3921cb0ef41Sopenharmony_ci<li><a href="#emitteroneventname-listener"><code>emitter.on(eventName, listener)</code></a></li> 3931cb0ef41Sopenharmony_ci<li><a href="#emitteronceeventname-listener"><code>emitter.once(eventName, listener)</code></a></li> 3941cb0ef41Sopenharmony_ci<li><a href="#emitterprependlistenereventname-listener"><code>emitter.prependListener(eventName, listener)</code></a></li> 3951cb0ef41Sopenharmony_ci<li><a href="#emitterprependoncelistenereventname-listener"><code>emitter.prependOnceListener(eventName, listener)</code></a></li> 3961cb0ef41Sopenharmony_ci<li><a href="#emitterremovealllistenerseventname"><code>emitter.removeAllListeners([eventName])</code></a></li> 3971cb0ef41Sopenharmony_ci<li><a href="#emitterremovelistenereventname-listener"><code>emitter.removeListener(eventName, listener)</code></a></li> 3981cb0ef41Sopenharmony_ci<li><a href="#emittersetmaxlistenersn"><code>emitter.setMaxListeners(n)</code></a></li> 3991cb0ef41Sopenharmony_ci<li><a href="#emitterrawlistenerseventname"><code>emitter.rawListeners(eventName)</code></a></li> 4001cb0ef41Sopenharmony_ci<li><a href="#emittersymbolfornodejsrejectionerr-eventname-args"><code>emitter[Symbol.for('nodejs.rejection')](err, eventName[, ...args])</code></a></li> 4011cb0ef41Sopenharmony_ci</ul> 4021cb0ef41Sopenharmony_ci</li> 4031cb0ef41Sopenharmony_ci<li><a href="#eventsdefaultmaxlisteners"><code>events.defaultMaxListeners</code></a></li> 4041cb0ef41Sopenharmony_ci<li><a href="#eventserrormonitor"><code>events.errorMonitor</code></a></li> 4051cb0ef41Sopenharmony_ci<li><a href="#eventsgeteventlistenersemitterortarget-eventname"><code>events.getEventListeners(emitterOrTarget, eventName)</code></a></li> 4061cb0ef41Sopenharmony_ci<li><a href="#eventsgetmaxlistenersemitterortarget"><code>events.getMaxListeners(emitterOrTarget)</code></a></li> 4071cb0ef41Sopenharmony_ci<li><a href="#eventsonceemitter-name-options"><code>events.once(emitter, name[, options])</code></a> 4081cb0ef41Sopenharmony_ci<ul> 4091cb0ef41Sopenharmony_ci<li><a href="#awaiting-multiple-events-emitted-on-processnexttick">Awaiting multiple events emitted on <code>process.nextTick()</code></a></li> 4101cb0ef41Sopenharmony_ci</ul> 4111cb0ef41Sopenharmony_ci</li> 4121cb0ef41Sopenharmony_ci<li><a href="#eventscapturerejections"><code>events.captureRejections</code></a></li> 4131cb0ef41Sopenharmony_ci<li><a href="#eventscapturerejectionsymbol"><code>events.captureRejectionSymbol</code></a></li> 4141cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#eventslistenercountemitter-eventname"><code>events.listenerCount(emitter, eventName)</code></a></span></li> 4151cb0ef41Sopenharmony_ci<li><a href="#eventsonemitter-eventname-options"><code>events.on(emitter, eventName[, options])</code></a></li> 4161cb0ef41Sopenharmony_ci<li><a href="#eventssetmaxlistenersn-eventtargets"><code>events.setMaxListeners(n[, ...eventTargets])</code></a></li> 4171cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#eventsaddabortlistenersignal-resource"><code>events.addAbortListener(signal, resource)</code></a></span></li> 4181cb0ef41Sopenharmony_ci<li><a href="#class-eventseventemitterasyncresource-extends-eventemitter">Class: <code>events.EventEmitterAsyncResource extends EventEmitter</code></a> 4191cb0ef41Sopenharmony_ci<ul> 4201cb0ef41Sopenharmony_ci<li><a href="#new-eventseventemitterasyncresourceoptions"><code>new events.EventEmitterAsyncResource([options])</code></a></li> 4211cb0ef41Sopenharmony_ci<li><a href="#eventemitterasyncresourceasyncid"><code>eventemitterasyncresource.asyncId</code></a></li> 4221cb0ef41Sopenharmony_ci<li><a href="#eventemitterasyncresourceasyncresource"><code>eventemitterasyncresource.asyncResource</code></a></li> 4231cb0ef41Sopenharmony_ci<li><a href="#eventemitterasyncresourceemitdestroy"><code>eventemitterasyncresource.emitDestroy()</code></a></li> 4241cb0ef41Sopenharmony_ci<li><a href="#eventemitterasyncresourcetriggerasyncid"><code>eventemitterasyncresource.triggerAsyncId</code></a></li> 4251cb0ef41Sopenharmony_ci</ul> 4261cb0ef41Sopenharmony_ci</li> 4271cb0ef41Sopenharmony_ci<li><a href="#eventtarget-and-event-api"><code>EventTarget</code> and <code>Event</code> API</a> 4281cb0ef41Sopenharmony_ci<ul> 4291cb0ef41Sopenharmony_ci<li><a href="#nodejs-eventtarget-vs-dom-eventtarget">Node.js <code>EventTarget</code> vs. DOM <code>EventTarget</code></a></li> 4301cb0ef41Sopenharmony_ci<li><a href="#nodeeventtarget-vs-eventemitter"><code>NodeEventTarget</code> vs. <code>EventEmitter</code></a></li> 4311cb0ef41Sopenharmony_ci<li><a href="#event-listener">Event listener</a></li> 4321cb0ef41Sopenharmony_ci<li><a href="#eventtarget-error-handling"><code>EventTarget</code> error handling</a></li> 4331cb0ef41Sopenharmony_ci<li><a href="#class-event">Class: <code>Event</code></a> 4341cb0ef41Sopenharmony_ci<ul> 4351cb0ef41Sopenharmony_ci<li><a href="#eventbubbles"><code>event.bubbles</code></a></li> 4361cb0ef41Sopenharmony_ci<li><span class="stability_3"><a href="#eventcancelbubble"><code>event.cancelBubble</code></a></span></li> 4371cb0ef41Sopenharmony_ci<li><a href="#eventcancelable"><code>event.cancelable</code></a></li> 4381cb0ef41Sopenharmony_ci<li><a href="#eventcomposed"><code>event.composed</code></a></li> 4391cb0ef41Sopenharmony_ci<li><a href="#eventcomposedpath"><code>event.composedPath()</code></a></li> 4401cb0ef41Sopenharmony_ci<li><a href="#eventcurrenttarget"><code>event.currentTarget</code></a></li> 4411cb0ef41Sopenharmony_ci<li><a href="#eventdefaultprevented"><code>event.defaultPrevented</code></a></li> 4421cb0ef41Sopenharmony_ci<li><a href="#eventeventphase"><code>event.eventPhase</code></a></li> 4431cb0ef41Sopenharmony_ci<li><a href="#eventistrusted"><code>event.isTrusted</code></a></li> 4441cb0ef41Sopenharmony_ci<li><a href="#eventpreventdefault"><code>event.preventDefault()</code></a></li> 4451cb0ef41Sopenharmony_ci<li><span class="stability_3"><a href="#eventreturnvalue"><code>event.returnValue</code></a></span></li> 4461cb0ef41Sopenharmony_ci<li><span class="stability_3"><a href="#eventsrcelement"><code>event.srcElement</code></a></span></li> 4471cb0ef41Sopenharmony_ci<li><a href="#eventstopimmediatepropagation"><code>event.stopImmediatePropagation()</code></a></li> 4481cb0ef41Sopenharmony_ci<li><a href="#eventstoppropagation"><code>event.stopPropagation()</code></a></li> 4491cb0ef41Sopenharmony_ci<li><a href="#eventtarget"><code>event.target</code></a></li> 4501cb0ef41Sopenharmony_ci<li><a href="#eventtimestamp"><code>event.timeStamp</code></a></li> 4511cb0ef41Sopenharmony_ci<li><a href="#eventtype"><code>event.type</code></a></li> 4521cb0ef41Sopenharmony_ci</ul> 4531cb0ef41Sopenharmony_ci</li> 4541cb0ef41Sopenharmony_ci<li><a href="#class-eventtarget">Class: <code>EventTarget</code></a> 4551cb0ef41Sopenharmony_ci<ul> 4561cb0ef41Sopenharmony_ci<li><a href="#eventtargetaddeventlistenertype-listener-options"><code>eventTarget.addEventListener(type, listener[, options])</code></a></li> 4571cb0ef41Sopenharmony_ci<li><a href="#eventtargetdispatcheventevent"><code>eventTarget.dispatchEvent(event)</code></a></li> 4581cb0ef41Sopenharmony_ci<li><a href="#eventtargetremoveeventlistenertype-listener-options"><code>eventTarget.removeEventListener(type, listener[, options])</code></a></li> 4591cb0ef41Sopenharmony_ci</ul> 4601cb0ef41Sopenharmony_ci</li> 4611cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#class-customevent">Class: <code>CustomEvent</code></a></span> 4621cb0ef41Sopenharmony_ci<ul> 4631cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#eventdetail"><code>event.detail</code></a></span></li> 4641cb0ef41Sopenharmony_ci</ul> 4651cb0ef41Sopenharmony_ci</li> 4661cb0ef41Sopenharmony_ci<li><a href="#class-nodeeventtarget">Class: <code>NodeEventTarget</code></a> 4671cb0ef41Sopenharmony_ci<ul> 4681cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargetaddlistenertype-listener"><code>nodeEventTarget.addListener(type, listener)</code></a></li> 4691cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargetemittype-arg"><code>nodeEventTarget.emit(type, arg)</code></a></li> 4701cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargeteventnames"><code>nodeEventTarget.eventNames()</code></a></li> 4711cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargetlistenercounttype"><code>nodeEventTarget.listenerCount(type)</code></a></li> 4721cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargetsetmaxlistenersn"><code>nodeEventTarget.setMaxListeners(n)</code></a></li> 4731cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargetgetmaxlisteners"><code>nodeEventTarget.getMaxListeners()</code></a></li> 4741cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargetofftype-listener-options"><code>nodeEventTarget.off(type, listener[, options])</code></a></li> 4751cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargetontype-listener"><code>nodeEventTarget.on(type, listener)</code></a></li> 4761cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargetoncetype-listener"><code>nodeEventTarget.once(type, listener)</code></a></li> 4771cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargetremovealllistenerstype"><code>nodeEventTarget.removeAllListeners([type])</code></a></li> 4781cb0ef41Sopenharmony_ci<li><a href="#nodeeventtargetremovelistenertype-listener-options"><code>nodeEventTarget.removeListener(type, listener[, options])</code></a></li> 4791cb0ef41Sopenharmony_ci</ul> 4801cb0ef41Sopenharmony_ci</li> 4811cb0ef41Sopenharmony_ci</ul> 4821cb0ef41Sopenharmony_ci</li> 4831cb0ef41Sopenharmony_ci</ul> 4841cb0ef41Sopenharmony_ci</li> 4851cb0ef41Sopenharmony_ci</ul></details> 4861cb0ef41Sopenharmony_ci 4871cb0ef41Sopenharmony_ci <div id="apicontent"> 4881cb0ef41Sopenharmony_ci <h2>Events<span><a class="mark" href="#events" id="events">#</a></span><a aria-hidden="true" class="legacy" id="events_events"></a></h2> 4891cb0ef41Sopenharmony_ci 4901cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 4911cb0ef41Sopenharmony_ci 4921cb0ef41Sopenharmony_ci<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/events.js">lib/events.js</a></p> 4931cb0ef41Sopenharmony_ci<p>Much of the Node.js core API is built around an idiomatic asynchronous 4941cb0ef41Sopenharmony_cievent-driven architecture in which certain kinds of objects (called "emitters") 4951cb0ef41Sopenharmony_ciemit named events that cause <code>Function</code> objects ("listeners") to be called.</p> 4961cb0ef41Sopenharmony_ci<p>For instance: a <a href="net.html#class-netserver"><code>net.Server</code></a> object emits an event each time a peer 4971cb0ef41Sopenharmony_ciconnects to it; a <a href="fs.html#class-fsreadstream"><code>fs.ReadStream</code></a> emits an event when the file is opened; 4981cb0ef41Sopenharmony_cia <a href="stream.html">stream</a> emits an event whenever data is available to be read.</p> 4991cb0ef41Sopenharmony_ci<p>All objects that emit events are instances of the <code>EventEmitter</code> class. These 5001cb0ef41Sopenharmony_ciobjects expose an <code>eventEmitter.on()</code> function that allows one or more 5011cb0ef41Sopenharmony_cifunctions to be attached to named events emitted by the object. Typically, 5021cb0ef41Sopenharmony_cievent names are camel-cased strings but any valid JavaScript property key 5031cb0ef41Sopenharmony_cican be used.</p> 5041cb0ef41Sopenharmony_ci<p>When the <code>EventEmitter</code> object emits an event, all of the functions attached 5051cb0ef41Sopenharmony_cito that specific event are called <em>synchronously</em>. Any values returned by the 5061cb0ef41Sopenharmony_cicalled listeners are <em>ignored</em> and discarded.</p> 5071cb0ef41Sopenharmony_ci<p>The following example shows a simple <code>EventEmitter</code> instance with a single 5081cb0ef41Sopenharmony_cilistener. The <code>eventEmitter.on()</code> method is used to register listeners, while 5091cb0ef41Sopenharmony_cithe <code>eventEmitter.emit()</code> method is used to trigger the event.</p> 5101cb0ef41Sopenharmony_ci 5111cb0ef41Sopenharmony_ci<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>; 5121cb0ef41Sopenharmony_ci 5131cb0ef41Sopenharmony_ci<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> {} 5141cb0ef41Sopenharmony_ci 5151cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 5161cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 5171cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'an event occurred!'</span>); 5181cb0ef41Sopenharmony_ci}); 5191cb0ef41Sopenharmony_cimyEmitter.<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>); 5201cb0ef41Sopenharmony_ci 5211cb0ef41Sopenharmony_ci<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> {} 5221cb0ef41Sopenharmony_ci 5231cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 5241cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 5251cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'an event occurred!'</span>); 5261cb0ef41Sopenharmony_ci}); 5271cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>);</code><button class="copy-button">copy</button></pre> 5281cb0ef41Sopenharmony_ci<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> 5291cb0ef41Sopenharmony_ci<p>The <code>eventEmitter.emit()</code> method allows an arbitrary set of arguments to be 5301cb0ef41Sopenharmony_cipassed to the listener functions. Keep in mind that when 5311cb0ef41Sopenharmony_cian ordinary listener function is called, the standard <code>this</code> keyword 5321cb0ef41Sopenharmony_ciis intentionally set to reference the <code>EventEmitter</code> instance to which the 5331cb0ef41Sopenharmony_cilistener is attached.</p> 5341cb0ef41Sopenharmony_ci 5351cb0ef41Sopenharmony_ci<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>; 5361cb0ef41Sopenharmony_ci<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> {} 5371cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 5381cb0ef41Sopenharmony_cimyEmitter.<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>) { 5391cb0ef41Sopenharmony_ci <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); 5401cb0ef41Sopenharmony_ci <span class="hljs-comment">// Prints:</span> 5411cb0ef41Sopenharmony_ci <span class="hljs-comment">// a b MyEmitter {</span> 5421cb0ef41Sopenharmony_ci <span class="hljs-comment">// _events: [Object: null prototype] { event: [Function (anonymous)] },</span> 5431cb0ef41Sopenharmony_ci <span class="hljs-comment">// _eventsCount: 1,</span> 5441cb0ef41Sopenharmony_ci <span class="hljs-comment">// _maxListeners: undefined,</span> 5451cb0ef41Sopenharmony_ci <span class="hljs-comment">// [Symbol(kCapture)]: false</span> 5461cb0ef41Sopenharmony_ci <span class="hljs-comment">// } true</span> 5471cb0ef41Sopenharmony_ci}); 5481cb0ef41Sopenharmony_cimyEmitter.<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>); 5491cb0ef41Sopenharmony_ci<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> {} 5501cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 5511cb0ef41Sopenharmony_cimyEmitter.<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>) { 5521cb0ef41Sopenharmony_ci <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); 5531cb0ef41Sopenharmony_ci <span class="hljs-comment">// Prints:</span> 5541cb0ef41Sopenharmony_ci <span class="hljs-comment">// a b MyEmitter {</span> 5551cb0ef41Sopenharmony_ci <span class="hljs-comment">// _events: [Object: null prototype] { event: [Function (anonymous)] },</span> 5561cb0ef41Sopenharmony_ci <span class="hljs-comment">// _eventsCount: 1,</span> 5571cb0ef41Sopenharmony_ci <span class="hljs-comment">// _maxListeners: undefined,</span> 5581cb0ef41Sopenharmony_ci <span class="hljs-comment">// [Symbol(kCapture)]: false</span> 5591cb0ef41Sopenharmony_ci <span class="hljs-comment">// } true</span> 5601cb0ef41Sopenharmony_ci}); 5611cb0ef41Sopenharmony_cimyEmitter.<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> 5621cb0ef41Sopenharmony_ci<p>It is possible to use ES6 Arrow Functions as listeners, however, when doing so, 5631cb0ef41Sopenharmony_cithe <code>this</code> keyword will no longer reference the <code>EventEmitter</code> instance:</p> 5641cb0ef41Sopenharmony_ci 5651cb0ef41Sopenharmony_ci<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>; 5661cb0ef41Sopenharmony_ci<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> {} 5671cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 5681cb0ef41Sopenharmony_cimyEmitter.<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> { 5691cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(a, b, <span class="hljs-variable language_">this</span>); 5701cb0ef41Sopenharmony_ci <span class="hljs-comment">// Prints: a b {}</span> 5711cb0ef41Sopenharmony_ci}); 5721cb0ef41Sopenharmony_cimyEmitter.<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>); 5731cb0ef41Sopenharmony_ci<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> {} 5741cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 5751cb0ef41Sopenharmony_cimyEmitter.<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> { 5761cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(a, b, <span class="hljs-variable language_">this</span>); 5771cb0ef41Sopenharmony_ci <span class="hljs-comment">// Prints: a b {}</span> 5781cb0ef41Sopenharmony_ci}); 5791cb0ef41Sopenharmony_cimyEmitter.<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> 5801cb0ef41Sopenharmony_ci</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> 5811cb0ef41Sopenharmony_ci<p>The <code>EventEmitter</code> calls all listeners synchronously in the order in which 5821cb0ef41Sopenharmony_cithey were registered. This ensures the proper sequencing of 5831cb0ef41Sopenharmony_cievents and helps avoid race conditions and logic errors. When appropriate, 5841cb0ef41Sopenharmony_cilistener functions can switch to an asynchronous mode of operation using 5851cb0ef41Sopenharmony_cithe <code>setImmediate()</code> or <code>process.nextTick()</code> methods:</p> 5861cb0ef41Sopenharmony_ci 5871cb0ef41Sopenharmony_ci<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>; 5881cb0ef41Sopenharmony_ci<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> {} 5891cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 5901cb0ef41Sopenharmony_cimyEmitter.<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> { 5911cb0ef41Sopenharmony_ci <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> { 5921cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'this happens asynchronously'</span>); 5931cb0ef41Sopenharmony_ci }); 5941cb0ef41Sopenharmony_ci}); 5951cb0ef41Sopenharmony_cimyEmitter.<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>); 5961cb0ef41Sopenharmony_ci<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> {} 5971cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 5981cb0ef41Sopenharmony_cimyEmitter.<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> { 5991cb0ef41Sopenharmony_ci <span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =></span> { 6001cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'this happens asynchronously'</span>); 6011cb0ef41Sopenharmony_ci }); 6021cb0ef41Sopenharmony_ci}); 6031cb0ef41Sopenharmony_cimyEmitter.<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> 6041cb0ef41Sopenharmony_ci</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> 6051cb0ef41Sopenharmony_ci<p>When a listener is registered using the <code>eventEmitter.on()</code> method, that 6061cb0ef41Sopenharmony_cilistener is invoked <em>every time</em> the named event is emitted.</p> 6071cb0ef41Sopenharmony_ci 6081cb0ef41Sopenharmony_ci<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>; 6091cb0ef41Sopenharmony_ci<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> {} 6101cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 6111cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> m = <span class="hljs-number">0</span>; 6121cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 6131cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(++m); 6141cb0ef41Sopenharmony_ci}); 6151cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 6161cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1</span> 6171cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 6181cb0ef41Sopenharmony_ci<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>); 6191cb0ef41Sopenharmony_ci<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> {} 6201cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 6211cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> m = <span class="hljs-number">0</span>; 6221cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 6231cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(++m); 6241cb0ef41Sopenharmony_ci}); 6251cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 6261cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1</span> 6271cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 6281cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 2</span></code><button class="copy-button">copy</button></pre> 6291cb0ef41Sopenharmony_ci<p>Using the <code>eventEmitter.once()</code> method, it is possible to register a listener 6301cb0ef41Sopenharmony_cithat is called at most once for a particular event. Once the event is emitted, 6311cb0ef41Sopenharmony_cithe listener is unregistered and <em>then</em> called.</p> 6321cb0ef41Sopenharmony_ci 6331cb0ef41Sopenharmony_ci<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>; 6341cb0ef41Sopenharmony_ci<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> {} 6351cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 6361cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> m = <span class="hljs-number">0</span>; 6371cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">once</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 6381cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(++m); 6391cb0ef41Sopenharmony_ci}); 6401cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 6411cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1</span> 6421cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 6431cb0ef41Sopenharmony_ci<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>); 6441cb0ef41Sopenharmony_ci<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> {} 6451cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 6461cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> m = <span class="hljs-number">0</span>; 6471cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">once</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 6481cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(++m); 6491cb0ef41Sopenharmony_ci}); 6501cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 6511cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 1</span> 6521cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 6531cb0ef41Sopenharmony_ci<span class="hljs-comment">// Ignored</span></code><button class="copy-button">copy</button></pre> 6541cb0ef41Sopenharmony_ci</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> 6551cb0ef41Sopenharmony_ci<p>When an error occurs within an <code>EventEmitter</code> instance, the typical action is 6561cb0ef41Sopenharmony_cifor an <code>'error'</code> event to be emitted. These are treated as special cases 6571cb0ef41Sopenharmony_ciwithin Node.js.</p> 6581cb0ef41Sopenharmony_ci<p>If an <code>EventEmitter</code> does <em>not</em> have at least one listener registered for the 6591cb0ef41Sopenharmony_ci<code>'error'</code> event, and an <code>'error'</code> event is emitted, the error is thrown, a 6601cb0ef41Sopenharmony_cistack trace is printed, and the Node.js process exits.</p> 6611cb0ef41Sopenharmony_ci 6621cb0ef41Sopenharmony_ci<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>; 6631cb0ef41Sopenharmony_ci<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> {} 6641cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 6651cb0ef41Sopenharmony_cimyEmitter.<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>)); 6661cb0ef41Sopenharmony_ci<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>); 6671cb0ef41Sopenharmony_ci<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> {} 6681cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 6691cb0ef41Sopenharmony_cimyEmitter.<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>)); 6701cb0ef41Sopenharmony_ci<span class="hljs-comment">// Throws and crashes Node.js</span></code><button class="copy-button">copy</button></pre> 6711cb0ef41Sopenharmony_ci<p>To guard against crashing the Node.js process the <a href="domain.html"><code>domain</code></a> module can be 6721cb0ef41Sopenharmony_ciused. (Note, however, that the <code>node:domain</code> module is deprecated.)</p> 6731cb0ef41Sopenharmony_ci<p>As a best practice, listeners should always be added for the <code>'error'</code> events.</p> 6741cb0ef41Sopenharmony_ci 6751cb0ef41Sopenharmony_ci<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>; 6761cb0ef41Sopenharmony_ci<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> {} 6771cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 6781cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 6791cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'whoops! there was an error'</span>); 6801cb0ef41Sopenharmony_ci}); 6811cb0ef41Sopenharmony_cimyEmitter.<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>)); 6821cb0ef41Sopenharmony_ci<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>); 6831cb0ef41Sopenharmony_ci<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> {} 6841cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 6851cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 6861cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'whoops! there was an error'</span>); 6871cb0ef41Sopenharmony_ci}); 6881cb0ef41Sopenharmony_cimyEmitter.<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>)); 6891cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: whoops! there was an error</span></code><button class="copy-button">copy</button></pre> 6901cb0ef41Sopenharmony_ci<p>It is possible to monitor <code>'error'</code> events without consuming the emitted error 6911cb0ef41Sopenharmony_ciby installing a listener using the symbol <code>events.errorMonitor</code>.</p> 6921cb0ef41Sopenharmony_ci 6931cb0ef41Sopenharmony_ci<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>; 6941cb0ef41Sopenharmony_ci 6951cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 6961cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">on</span>(errorMonitor, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 6971cb0ef41Sopenharmony_ci <span class="hljs-title class_">MyMonitoringTool</span>.<span class="hljs-title function_">log</span>(err); 6981cb0ef41Sopenharmony_ci}); 6991cb0ef41Sopenharmony_cimyEmitter.<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>)); 7001cb0ef41Sopenharmony_ci<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>); 7011cb0ef41Sopenharmony_ci 7021cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 7031cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">on</span>(errorMonitor, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 7041cb0ef41Sopenharmony_ci <span class="hljs-title class_">MyMonitoringTool</span>.<span class="hljs-title function_">log</span>(err); 7051cb0ef41Sopenharmony_ci}); 7061cb0ef41Sopenharmony_cimyEmitter.<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>)); 7071cb0ef41Sopenharmony_ci<span class="hljs-comment">// Still throws and crashes Node.js</span></code><button class="copy-button">copy</button></pre> 7081cb0ef41Sopenharmony_ci</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> 7091cb0ef41Sopenharmony_ci<p>Using <code>async</code> functions with event handlers is problematic, because it 7101cb0ef41Sopenharmony_cican lead to an unhandled rejection in case of a thrown exception:</p> 7111cb0ef41Sopenharmony_ci 7121cb0ef41Sopenharmony_ci<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>; 7131cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 7141cb0ef41Sopenharmony_ciee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span class="hljs-keyword">async</span> (value) => { 7151cb0ef41Sopenharmony_ci <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>); 7161cb0ef41Sopenharmony_ci});</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>); 7171cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 7181cb0ef41Sopenharmony_ciee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span class="hljs-keyword">async</span> (value) => { 7191cb0ef41Sopenharmony_ci <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>); 7201cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 7211cb0ef41Sopenharmony_ci<p>The <code>captureRejections</code> option in the <code>EventEmitter</code> constructor or the global 7221cb0ef41Sopenharmony_cisetting change this behavior, installing a <code>.then(undefined, handler)</code> 7231cb0ef41Sopenharmony_cihandler on the <code>Promise</code>. This handler routes the exception 7241cb0ef41Sopenharmony_ciasynchronously to the <a href="#emittersymbolfornodejsrejectionerr-eventname-args"><code>Symbol.for('nodejs.rejection')</code></a> method 7251cb0ef41Sopenharmony_ciif there is one, or to <a href="#error-events"><code>'error'</code></a> event handler if there is none.</p> 7261cb0ef41Sopenharmony_ci 7271cb0ef41Sopenharmony_ci<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>; 7281cb0ef41Sopenharmony_ci<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> }); 7291cb0ef41Sopenharmony_ciee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span class="hljs-keyword">async</span> (value) => { 7301cb0ef41Sopenharmony_ci <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>); 7311cb0ef41Sopenharmony_ci}); 7321cb0ef41Sopenharmony_ci 7331cb0ef41Sopenharmony_ciee1.<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>); 7341cb0ef41Sopenharmony_ci 7351cb0ef41Sopenharmony_ci<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> }); 7361cb0ef41Sopenharmony_ciee2.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span class="hljs-keyword">async</span> (value) => { 7371cb0ef41Sopenharmony_ci <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>); 7381cb0ef41Sopenharmony_ci}); 7391cb0ef41Sopenharmony_ci 7401cb0ef41Sopenharmony_ciee2[<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>); 7411cb0ef41Sopenharmony_ci<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> }); 7421cb0ef41Sopenharmony_ciee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span class="hljs-keyword">async</span> (value) => { 7431cb0ef41Sopenharmony_ci <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>); 7441cb0ef41Sopenharmony_ci}); 7451cb0ef41Sopenharmony_ci 7461cb0ef41Sopenharmony_ciee1.<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>); 7471cb0ef41Sopenharmony_ci 7481cb0ef41Sopenharmony_ci<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> }); 7491cb0ef41Sopenharmony_ciee2.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span class="hljs-keyword">async</span> (value) => { 7501cb0ef41Sopenharmony_ci <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>); 7511cb0ef41Sopenharmony_ci}); 7521cb0ef41Sopenharmony_ci 7531cb0ef41Sopenharmony_ciee2[<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> 7541cb0ef41Sopenharmony_ci<p>Setting <code>events.captureRejections = true</code> will change the default for all 7551cb0ef41Sopenharmony_cinew instances of <code>EventEmitter</code>.</p> 7561cb0ef41Sopenharmony_ci 7571cb0ef41Sopenharmony_ci<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>; 7581cb0ef41Sopenharmony_ci 7591cb0ef41Sopenharmony_ci<span class="hljs-title class_">EventEmitter</span>.<span class="hljs-property">captureRejections</span> = <span class="hljs-literal">true</span>; 7601cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ee1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 7611cb0ef41Sopenharmony_ciee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span class="hljs-keyword">async</span> (value) => { 7621cb0ef41Sopenharmony_ci <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>); 7631cb0ef41Sopenharmony_ci}); 7641cb0ef41Sopenharmony_ci 7651cb0ef41Sopenharmony_ciee1.<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>); 7661cb0ef41Sopenharmony_cievents.<span class="hljs-property">captureRejections</span> = <span class="hljs-literal">true</span>; 7671cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ee1 = <span class="hljs-keyword">new</span> events.<span class="hljs-title class_">EventEmitter</span>(); 7681cb0ef41Sopenharmony_ciee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'something'</span>, <span class="hljs-keyword">async</span> (value) => { 7691cb0ef41Sopenharmony_ci <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>); 7701cb0ef41Sopenharmony_ci}); 7711cb0ef41Sopenharmony_ci 7721cb0ef41Sopenharmony_ciee1.<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> 7731cb0ef41Sopenharmony_ci<p>The <code>'error'</code> events that are generated by the <code>captureRejections</code> behavior 7741cb0ef41Sopenharmony_cido not have a catch handler to avoid infinite error loops: the 7751cb0ef41Sopenharmony_cirecommendation is to <strong>not use <code>async</code> functions as <code>'error'</code> event handlers</strong>.</p> 7761cb0ef41Sopenharmony_ci</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> 7771cb0ef41Sopenharmony_ci<div class="api_metadata"> 7781cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 7791cb0ef41Sopenharmony_ci<table> 7801cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 7811cb0ef41Sopenharmony_ci<tr><td>v13.4.0, v12.16.0</td> 7821cb0ef41Sopenharmony_ci<td><p>Added captureRejections option.</p></td></tr> 7831cb0ef41Sopenharmony_ci<tr><td>v0.1.26</td> 7841cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.26</span></p></td></tr> 7851cb0ef41Sopenharmony_ci</tbody></table> 7861cb0ef41Sopenharmony_ci</details> 7871cb0ef41Sopenharmony_ci</div> 7881cb0ef41Sopenharmony_ci<p>The <code>EventEmitter</code> class is defined and exposed by the <code>node:events</code> module:</p> 7891cb0ef41Sopenharmony_ci 7901cb0ef41Sopenharmony_ci<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> 7911cb0ef41Sopenharmony_ci<p>All <code>EventEmitter</code>s emit the event <code>'newListener'</code> when new listeners are 7921cb0ef41Sopenharmony_ciadded and <code>'removeListener'</code> when existing listeners are removed.</p> 7931cb0ef41Sopenharmony_ci<p>It supports the following option:</p> 7941cb0ef41Sopenharmony_ci<ul> 7951cb0ef41Sopenharmony_ci<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 7961cb0ef41Sopenharmony_ci<a href="#capture-rejections-of-promises">automatic capturing of promise rejection</a>. 7971cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li> 7981cb0ef41Sopenharmony_ci</ul> 7991cb0ef41Sopenharmony_ci<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> 8001cb0ef41Sopenharmony_ci<div class="api_metadata"> 8011cb0ef41Sopenharmony_ci<span>Added in: v0.1.26</span> 8021cb0ef41Sopenharmony_ci</div> 8031cb0ef41Sopenharmony_ci<ul> 8041cb0ef41Sopenharmony_ci<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> 8051cb0ef41Sopenharmony_ci<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> 8061cb0ef41Sopenharmony_ci</ul> 8071cb0ef41Sopenharmony_ci<p>The <code>EventEmitter</code> instance will emit its own <code>'newListener'</code> event <em>before</em> 8081cb0ef41Sopenharmony_cia listener is added to its internal array of listeners.</p> 8091cb0ef41Sopenharmony_ci<p>Listeners registered for the <code>'newListener'</code> event are passed the event 8101cb0ef41Sopenharmony_ciname and a reference to the listener being added.</p> 8111cb0ef41Sopenharmony_ci<p>The fact that the event is triggered before adding the listener has a subtle 8121cb0ef41Sopenharmony_cibut important side effect: any <em>additional</em> listeners registered to the same 8131cb0ef41Sopenharmony_ci<code>name</code> <em>within</em> the <code>'newListener'</code> callback are inserted <em>before</em> the 8141cb0ef41Sopenharmony_cilistener that is in the process of being added.</p> 8151cb0ef41Sopenharmony_ci 8161cb0ef41Sopenharmony_ci<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>; 8171cb0ef41Sopenharmony_ci<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> {} 8181cb0ef41Sopenharmony_ci 8191cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 8201cb0ef41Sopenharmony_ci<span class="hljs-comment">// Only do this once so we don't loop forever</span> 8211cb0ef41Sopenharmony_cimyEmitter.<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> { 8221cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (event === <span class="hljs-string">'event'</span>) { 8231cb0ef41Sopenharmony_ci <span class="hljs-comment">// Insert a new listener in front</span> 8241cb0ef41Sopenharmony_ci myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 8251cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'B'</span>); 8261cb0ef41Sopenharmony_ci }); 8271cb0ef41Sopenharmony_ci } 8281cb0ef41Sopenharmony_ci}); 8291cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 8301cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'A'</span>); 8311cb0ef41Sopenharmony_ci}); 8321cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 8331cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 8341cb0ef41Sopenharmony_ci<span class="hljs-comment">// B</span> 8351cb0ef41Sopenharmony_ci<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>); 8361cb0ef41Sopenharmony_ci<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> {} 8371cb0ef41Sopenharmony_ci 8381cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 8391cb0ef41Sopenharmony_ci<span class="hljs-comment">// Only do this once so we don't loop forever</span> 8401cb0ef41Sopenharmony_cimyEmitter.<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> { 8411cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (event === <span class="hljs-string">'event'</span>) { 8421cb0ef41Sopenharmony_ci <span class="hljs-comment">// Insert a new listener in front</span> 8431cb0ef41Sopenharmony_ci myEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 8441cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'B'</span>); 8451cb0ef41Sopenharmony_ci }); 8461cb0ef41Sopenharmony_ci } 8471cb0ef41Sopenharmony_ci}); 8481cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 8491cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'A'</span>); 8501cb0ef41Sopenharmony_ci}); 8511cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 8521cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 8531cb0ef41Sopenharmony_ci<span class="hljs-comment">// B</span> 8541cb0ef41Sopenharmony_ci<span class="hljs-comment">// A</span></code><button class="copy-button">copy</button></pre> 8551cb0ef41Sopenharmony_ci<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> 8561cb0ef41Sopenharmony_ci<div class="api_metadata"> 8571cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 8581cb0ef41Sopenharmony_ci<table> 8591cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 8601cb0ef41Sopenharmony_ci<tr><td>v6.1.0, v4.7.0</td> 8611cb0ef41Sopenharmony_ci<td><p>For listeners attached using <code>.once()</code>, the <code>listener</code> argument now yields the original listener function.</p></td></tr> 8621cb0ef41Sopenharmony_ci<tr><td>v0.9.3</td> 8631cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.9.3</span></p></td></tr> 8641cb0ef41Sopenharmony_ci</tbody></table> 8651cb0ef41Sopenharmony_ci</details> 8661cb0ef41Sopenharmony_ci</div> 8671cb0ef41Sopenharmony_ci<ul> 8681cb0ef41Sopenharmony_ci<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> 8691cb0ef41Sopenharmony_ci<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> 8701cb0ef41Sopenharmony_ci</ul> 8711cb0ef41Sopenharmony_ci<p>The <code>'removeListener'</code> event is emitted <em>after</em> the <code>listener</code> is removed.</p> 8721cb0ef41Sopenharmony_ci<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> 8731cb0ef41Sopenharmony_ci<div class="api_metadata"> 8741cb0ef41Sopenharmony_ci<span>Added in: v0.1.26</span> 8751cb0ef41Sopenharmony_ci</div> 8761cb0ef41Sopenharmony_ci<ul> 8771cb0ef41Sopenharmony_ci<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> 8781cb0ef41Sopenharmony_ci<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 8791cb0ef41Sopenharmony_ci</ul> 8801cb0ef41Sopenharmony_ci<p>Alias for <code>emitter.on(eventName, listener)</code>.</p> 8811cb0ef41Sopenharmony_ci<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> 8821cb0ef41Sopenharmony_ci<div class="api_metadata"> 8831cb0ef41Sopenharmony_ci<span>Added in: v0.1.26</span> 8841cb0ef41Sopenharmony_ci</div> 8851cb0ef41Sopenharmony_ci<ul> 8861cb0ef41Sopenharmony_ci<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> 8871cb0ef41Sopenharmony_ci<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 8881cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 8891cb0ef41Sopenharmony_ci</ul> 8901cb0ef41Sopenharmony_ci<p>Synchronously calls each of the listeners registered for the event named 8911cb0ef41Sopenharmony_ci<code>eventName</code>, in the order they were registered, passing the supplied arguments 8921cb0ef41Sopenharmony_cito each.</p> 8931cb0ef41Sopenharmony_ci<p>Returns <code>true</code> if the event had listeners, <code>false</code> otherwise.</p> 8941cb0ef41Sopenharmony_ci 8951cb0ef41Sopenharmony_ci<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>; 8961cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 8971cb0ef41Sopenharmony_ci 8981cb0ef41Sopenharmony_ci<span class="hljs-comment">// First listener</span> 8991cb0ef41Sopenharmony_cimyEmitter.<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>) { 9001cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Helloooo! first listener'</span>); 9011cb0ef41Sopenharmony_ci}); 9021cb0ef41Sopenharmony_ci<span class="hljs-comment">// Second listener</span> 9031cb0ef41Sopenharmony_cimyEmitter.<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>) { 9041cb0ef41Sopenharmony_ci <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>); 9051cb0ef41Sopenharmony_ci}); 9061cb0ef41Sopenharmony_ci<span class="hljs-comment">// Third listener</span> 9071cb0ef41Sopenharmony_cimyEmitter.<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>) { 9081cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> parameters = args.<span class="hljs-title function_">join</span>(<span class="hljs-string">', '</span>); 9091cb0ef41Sopenharmony_ci <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>); 9101cb0ef41Sopenharmony_ci}); 9111cb0ef41Sopenharmony_ci 9121cb0ef41Sopenharmony_ci<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>)); 9131cb0ef41Sopenharmony_ci 9141cb0ef41Sopenharmony_cimyEmitter.<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>); 9151cb0ef41Sopenharmony_ci 9161cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 9171cb0ef41Sopenharmony_ci<span class="hljs-comment">// [</span> 9181cb0ef41Sopenharmony_ci<span class="hljs-comment">// [Function: firstListener],</span> 9191cb0ef41Sopenharmony_ci<span class="hljs-comment">// [Function: secondListener],</span> 9201cb0ef41Sopenharmony_ci<span class="hljs-comment">// [Function: thirdListener]</span> 9211cb0ef41Sopenharmony_ci<span class="hljs-comment">// ]</span> 9221cb0ef41Sopenharmony_ci<span class="hljs-comment">// Helloooo! first listener</span> 9231cb0ef41Sopenharmony_ci<span class="hljs-comment">// event with parameters 1, 2 in second listener</span> 9241cb0ef41Sopenharmony_ci<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>); 9251cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 9261cb0ef41Sopenharmony_ci 9271cb0ef41Sopenharmony_ci<span class="hljs-comment">// First listener</span> 9281cb0ef41Sopenharmony_cimyEmitter.<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>) { 9291cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Helloooo! first listener'</span>); 9301cb0ef41Sopenharmony_ci}); 9311cb0ef41Sopenharmony_ci<span class="hljs-comment">// Second listener</span> 9321cb0ef41Sopenharmony_cimyEmitter.<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>) { 9331cb0ef41Sopenharmony_ci <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>); 9341cb0ef41Sopenharmony_ci}); 9351cb0ef41Sopenharmony_ci<span class="hljs-comment">// Third listener</span> 9361cb0ef41Sopenharmony_cimyEmitter.<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>) { 9371cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> parameters = args.<span class="hljs-title function_">join</span>(<span class="hljs-string">', '</span>); 9381cb0ef41Sopenharmony_ci <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>); 9391cb0ef41Sopenharmony_ci}); 9401cb0ef41Sopenharmony_ci 9411cb0ef41Sopenharmony_ci<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>)); 9421cb0ef41Sopenharmony_ci 9431cb0ef41Sopenharmony_cimyEmitter.<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>); 9441cb0ef41Sopenharmony_ci 9451cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 9461cb0ef41Sopenharmony_ci<span class="hljs-comment">// [</span> 9471cb0ef41Sopenharmony_ci<span class="hljs-comment">// [Function: firstListener],</span> 9481cb0ef41Sopenharmony_ci<span class="hljs-comment">// [Function: secondListener],</span> 9491cb0ef41Sopenharmony_ci<span class="hljs-comment">// [Function: thirdListener]</span> 9501cb0ef41Sopenharmony_ci<span class="hljs-comment">// ]</span> 9511cb0ef41Sopenharmony_ci<span class="hljs-comment">// Helloooo! first listener</span> 9521cb0ef41Sopenharmony_ci<span class="hljs-comment">// event with parameters 1, 2 in second listener</span> 9531cb0ef41Sopenharmony_ci<span class="hljs-comment">// event with parameters 1, 2, 3, 4, 5 in third listener</span></code><button class="copy-button">copy</button></pre> 9541cb0ef41Sopenharmony_ci<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> 9551cb0ef41Sopenharmony_ci<div class="api_metadata"> 9561cb0ef41Sopenharmony_ci<span>Added in: v6.0.0</span> 9571cb0ef41Sopenharmony_ci</div> 9581cb0ef41Sopenharmony_ci<ul> 9591cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a></li> 9601cb0ef41Sopenharmony_ci</ul> 9611cb0ef41Sopenharmony_ci<p>Returns an array listing the events for which the emitter has registered 9621cb0ef41Sopenharmony_cilisteners. The values in the array are strings or <code>Symbol</code>s.</p> 9631cb0ef41Sopenharmony_ci 9641cb0ef41Sopenharmony_ci<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>; 9651cb0ef41Sopenharmony_ci 9661cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 9671cb0ef41Sopenharmony_cimyEE.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">() =></span> {}); 9681cb0ef41Sopenharmony_cimyEE.<span class="hljs-title function_">on</span>(<span class="hljs-string">'bar'</span>, <span class="hljs-function">() =></span> {}); 9691cb0ef41Sopenharmony_ci 9701cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> sym = <span class="hljs-title class_">Symbol</span>(<span class="hljs-string">'symbol'</span>); 9711cb0ef41Sopenharmony_cimyEE.<span class="hljs-title function_">on</span>(sym, <span class="hljs-function">() =></span> {}); 9721cb0ef41Sopenharmony_ci 9731cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myEE.<span class="hljs-title function_">eventNames</span>()); 9741cb0ef41Sopenharmony_ci<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>); 9751cb0ef41Sopenharmony_ci 9761cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 9771cb0ef41Sopenharmony_cimyEE.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">() =></span> {}); 9781cb0ef41Sopenharmony_cimyEE.<span class="hljs-title function_">on</span>(<span class="hljs-string">'bar'</span>, <span class="hljs-function">() =></span> {}); 9791cb0ef41Sopenharmony_ci 9801cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> sym = <span class="hljs-title class_">Symbol</span>(<span class="hljs-string">'symbol'</span>); 9811cb0ef41Sopenharmony_cimyEE.<span class="hljs-title function_">on</span>(sym, <span class="hljs-function">() =></span> {}); 9821cb0ef41Sopenharmony_ci 9831cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myEE.<span class="hljs-title function_">eventNames</span>()); 9841cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: [ 'foo', 'bar', Symbol(symbol) ]</span></code><button class="copy-button">copy</button></pre> 9851cb0ef41Sopenharmony_ci<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> 9861cb0ef41Sopenharmony_ci<div class="api_metadata"> 9871cb0ef41Sopenharmony_ci<span>Added in: v1.0.0</span> 9881cb0ef41Sopenharmony_ci</div> 9891cb0ef41Sopenharmony_ci<ul> 9901cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 9911cb0ef41Sopenharmony_ci</ul> 9921cb0ef41Sopenharmony_ci<p>Returns the current max listener value for the <code>EventEmitter</code> which is either 9931cb0ef41Sopenharmony_ciset by <a href="#emittersetmaxlistenersn"><code>emitter.setMaxListeners(n)</code></a> or defaults to 9941cb0ef41Sopenharmony_ci<a href="#eventsdefaultmaxlisteners"><code>events.defaultMaxListeners</code></a>.</p> 9951cb0ef41Sopenharmony_ci<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> 9961cb0ef41Sopenharmony_ci<div class="api_metadata"> 9971cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 9981cb0ef41Sopenharmony_ci<table> 9991cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 10001cb0ef41Sopenharmony_ci<tr><td>v18.16.0</td> 10011cb0ef41Sopenharmony_ci<td><p>Added the <code>listener</code> argument.</p></td></tr> 10021cb0ef41Sopenharmony_ci<tr><td>v3.2.0</td> 10031cb0ef41Sopenharmony_ci<td><p><span>Added in: v3.2.0</span></p></td></tr> 10041cb0ef41Sopenharmony_ci</tbody></table> 10051cb0ef41Sopenharmony_ci</details> 10061cb0ef41Sopenharmony_ci</div> 10071cb0ef41Sopenharmony_ci<ul> 10081cb0ef41Sopenharmony_ci<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> 10091cb0ef41Sopenharmony_ci<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> 10101cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 10111cb0ef41Sopenharmony_ci</ul> 10121cb0ef41Sopenharmony_ci<p>Returns the number of listeners listening for the event named <code>eventName</code>. 10131cb0ef41Sopenharmony_ciIf <code>listener</code> is provided, it will return how many times the listener is found 10141cb0ef41Sopenharmony_ciin the list of the listeners of the event.</p> 10151cb0ef41Sopenharmony_ci<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> 10161cb0ef41Sopenharmony_ci<div class="api_metadata"> 10171cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 10181cb0ef41Sopenharmony_ci<table> 10191cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 10201cb0ef41Sopenharmony_ci<tr><td>v7.0.0</td> 10211cb0ef41Sopenharmony_ci<td><p>For listeners attached using <code>.once()</code> this returns the original listeners instead of wrapper functions now.</p></td></tr> 10221cb0ef41Sopenharmony_ci<tr><td>v0.1.26</td> 10231cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.26</span></p></td></tr> 10241cb0ef41Sopenharmony_ci</tbody></table> 10251cb0ef41Sopenharmony_ci</details> 10261cb0ef41Sopenharmony_ci</div> 10271cb0ef41Sopenharmony_ci<ul> 10281cb0ef41Sopenharmony_ci<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> 10291cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function[]></a></li> 10301cb0ef41Sopenharmony_ci</ul> 10311cb0ef41Sopenharmony_ci<p>Returns a copy of the array of listeners for the event named <code>eventName</code>.</p> 10321cb0ef41Sopenharmony_ci<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> { 10331cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'someone connected!'</span>); 10341cb0ef41Sopenharmony_ci}); 10351cb0ef41Sopenharmony_ci<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>))); 10361cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: [ [Function] ]</span></code> <button class="copy-button">copy</button></pre> 10371cb0ef41Sopenharmony_ci<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> 10381cb0ef41Sopenharmony_ci<div class="api_metadata"> 10391cb0ef41Sopenharmony_ci<span>Added in: v10.0.0</span> 10401cb0ef41Sopenharmony_ci</div> 10411cb0ef41Sopenharmony_ci<ul> 10421cb0ef41Sopenharmony_ci<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> 10431cb0ef41Sopenharmony_ci<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 10441cb0ef41Sopenharmony_ci<li>Returns: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 10451cb0ef41Sopenharmony_ci</ul> 10461cb0ef41Sopenharmony_ci<p>Alias for <a href="#emitterremovelistenereventname-listener"><code>emitter.removeListener()</code></a>.</p> 10471cb0ef41Sopenharmony_ci<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> 10481cb0ef41Sopenharmony_ci<div class="api_metadata"> 10491cb0ef41Sopenharmony_ci<span>Added in: v0.1.101</span> 10501cb0ef41Sopenharmony_ci</div> 10511cb0ef41Sopenharmony_ci<ul> 10521cb0ef41Sopenharmony_ci<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> 10531cb0ef41Sopenharmony_ci<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> 10541cb0ef41Sopenharmony_ci<li>Returns: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 10551cb0ef41Sopenharmony_ci</ul> 10561cb0ef41Sopenharmony_ci<p>Adds the <code>listener</code> function to the end of the listeners array for the 10571cb0ef41Sopenharmony_cievent named <code>eventName</code>. No checks are made to see if the <code>listener</code> has 10581cb0ef41Sopenharmony_cialready been added. Multiple calls passing the same combination of <code>eventName</code> 10591cb0ef41Sopenharmony_ciand <code>listener</code> will result in the <code>listener</code> being added, and called, multiple 10601cb0ef41Sopenharmony_citimes.</p> 10611cb0ef41Sopenharmony_ci<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> { 10621cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'someone connected!'</span>); 10631cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 10641cb0ef41Sopenharmony_ci<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p> 10651cb0ef41Sopenharmony_ci<p>By default, event listeners are invoked in the order they are added. The 10661cb0ef41Sopenharmony_ci<code>emitter.prependListener()</code> method can be used as an alternative to add the 10671cb0ef41Sopenharmony_cievent listener to the beginning of the listeners array.</p> 10681cb0ef41Sopenharmony_ci 10691cb0ef41Sopenharmony_ci<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>; 10701cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 10711cb0ef41Sopenharmony_cimyEE.<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>)); 10721cb0ef41Sopenharmony_cimyEE.<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>)); 10731cb0ef41Sopenharmony_cimyEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 10741cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 10751cb0ef41Sopenharmony_ci<span class="hljs-comment">// b</span> 10761cb0ef41Sopenharmony_ci<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>); 10771cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 10781cb0ef41Sopenharmony_cimyEE.<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>)); 10791cb0ef41Sopenharmony_cimyEE.<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>)); 10801cb0ef41Sopenharmony_cimyEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 10811cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 10821cb0ef41Sopenharmony_ci<span class="hljs-comment">// b</span> 10831cb0ef41Sopenharmony_ci<span class="hljs-comment">// a</span></code><button class="copy-button">copy</button></pre> 10841cb0ef41Sopenharmony_ci<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> 10851cb0ef41Sopenharmony_ci<div class="api_metadata"> 10861cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span> 10871cb0ef41Sopenharmony_ci</div> 10881cb0ef41Sopenharmony_ci<ul> 10891cb0ef41Sopenharmony_ci<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> 10901cb0ef41Sopenharmony_ci<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> 10911cb0ef41Sopenharmony_ci<li>Returns: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 10921cb0ef41Sopenharmony_ci</ul> 10931cb0ef41Sopenharmony_ci<p>Adds a <strong>one-time</strong> <code>listener</code> function for the event named <code>eventName</code>. The 10941cb0ef41Sopenharmony_cinext time <code>eventName</code> is triggered, this listener is removed and then invoked.</p> 10951cb0ef41Sopenharmony_ci<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> { 10961cb0ef41Sopenharmony_ci <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>); 10971cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 10981cb0ef41Sopenharmony_ci<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p> 10991cb0ef41Sopenharmony_ci<p>By default, event listeners are invoked in the order they are added. The 11001cb0ef41Sopenharmony_ci<code>emitter.prependOnceListener()</code> method can be used as an alternative to add the 11011cb0ef41Sopenharmony_cievent listener to the beginning of the listeners array.</p> 11021cb0ef41Sopenharmony_ci 11031cb0ef41Sopenharmony_ci<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>; 11041cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 11051cb0ef41Sopenharmony_cimyEE.<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>)); 11061cb0ef41Sopenharmony_cimyEE.<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>)); 11071cb0ef41Sopenharmony_cimyEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 11081cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 11091cb0ef41Sopenharmony_ci<span class="hljs-comment">// b</span> 11101cb0ef41Sopenharmony_ci<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>); 11111cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 11121cb0ef41Sopenharmony_cimyEE.<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>)); 11131cb0ef41Sopenharmony_cimyEE.<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>)); 11141cb0ef41Sopenharmony_cimyEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 11151cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 11161cb0ef41Sopenharmony_ci<span class="hljs-comment">// b</span> 11171cb0ef41Sopenharmony_ci<span class="hljs-comment">// a</span></code><button class="copy-button">copy</button></pre> 11181cb0ef41Sopenharmony_ci<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> 11191cb0ef41Sopenharmony_ci<div class="api_metadata"> 11201cb0ef41Sopenharmony_ci<span>Added in: v6.0.0</span> 11211cb0ef41Sopenharmony_ci</div> 11221cb0ef41Sopenharmony_ci<ul> 11231cb0ef41Sopenharmony_ci<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> 11241cb0ef41Sopenharmony_ci<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> 11251cb0ef41Sopenharmony_ci<li>Returns: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 11261cb0ef41Sopenharmony_ci</ul> 11271cb0ef41Sopenharmony_ci<p>Adds the <code>listener</code> function to the <em>beginning</em> of the listeners array for the 11281cb0ef41Sopenharmony_cievent named <code>eventName</code>. No checks are made to see if the <code>listener</code> has 11291cb0ef41Sopenharmony_cialready been added. Multiple calls passing the same combination of <code>eventName</code> 11301cb0ef41Sopenharmony_ciand <code>listener</code> will result in the <code>listener</code> being added, and called, multiple 11311cb0ef41Sopenharmony_citimes.</p> 11321cb0ef41Sopenharmony_ci<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> { 11331cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'someone connected!'</span>); 11341cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 11351cb0ef41Sopenharmony_ci<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p> 11361cb0ef41Sopenharmony_ci<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> 11371cb0ef41Sopenharmony_ci<div class="api_metadata"> 11381cb0ef41Sopenharmony_ci<span>Added in: v6.0.0</span> 11391cb0ef41Sopenharmony_ci</div> 11401cb0ef41Sopenharmony_ci<ul> 11411cb0ef41Sopenharmony_ci<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> 11421cb0ef41Sopenharmony_ci<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> 11431cb0ef41Sopenharmony_ci<li>Returns: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 11441cb0ef41Sopenharmony_ci</ul> 11451cb0ef41Sopenharmony_ci<p>Adds a <strong>one-time</strong> <code>listener</code> function for the event named <code>eventName</code> to the 11461cb0ef41Sopenharmony_ci<em>beginning</em> of the listeners array. The next time <code>eventName</code> is triggered, this 11471cb0ef41Sopenharmony_cilistener is removed, and then invoked.</p> 11481cb0ef41Sopenharmony_ci<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> { 11491cb0ef41Sopenharmony_ci <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>); 11501cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 11511cb0ef41Sopenharmony_ci<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p> 11521cb0ef41Sopenharmony_ci<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> 11531cb0ef41Sopenharmony_ci<div class="api_metadata"> 11541cb0ef41Sopenharmony_ci<span>Added in: v0.1.26</span> 11551cb0ef41Sopenharmony_ci</div> 11561cb0ef41Sopenharmony_ci<ul> 11571cb0ef41Sopenharmony_ci<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> 11581cb0ef41Sopenharmony_ci<li>Returns: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 11591cb0ef41Sopenharmony_ci</ul> 11601cb0ef41Sopenharmony_ci<p>Removes all listeners, or those of the specified <code>eventName</code>.</p> 11611cb0ef41Sopenharmony_ci<p>It is bad practice to remove listeners added elsewhere in the code, 11621cb0ef41Sopenharmony_ciparticularly when the <code>EventEmitter</code> instance was created by some other 11631cb0ef41Sopenharmony_cicomponent or module (e.g. sockets or file streams).</p> 11641cb0ef41Sopenharmony_ci<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p> 11651cb0ef41Sopenharmony_ci<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> 11661cb0ef41Sopenharmony_ci<div class="api_metadata"> 11671cb0ef41Sopenharmony_ci<span>Added in: v0.1.26</span> 11681cb0ef41Sopenharmony_ci</div> 11691cb0ef41Sopenharmony_ci<ul> 11701cb0ef41Sopenharmony_ci<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> 11711cb0ef41Sopenharmony_ci<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 11721cb0ef41Sopenharmony_ci<li>Returns: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 11731cb0ef41Sopenharmony_ci</ul> 11741cb0ef41Sopenharmony_ci<p>Removes the specified <code>listener</code> from the listener array for the event named 11751cb0ef41Sopenharmony_ci<code>eventName</code>.</p> 11761cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> <span class="hljs-title function_">callback</span> = (<span class="hljs-params">stream</span>) => { 11771cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'someone connected!'</span>); 11781cb0ef41Sopenharmony_ci}; 11791cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">on</span>(<span class="hljs-string">'connection'</span>, callback); 11801cb0ef41Sopenharmony_ci<span class="hljs-comment">// ...</span> 11811cb0ef41Sopenharmony_ciserver.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'connection'</span>, callback);</code> <button class="copy-button">copy</button></pre> 11821cb0ef41Sopenharmony_ci<p><code>removeListener()</code> will remove, at most, one instance of a listener from the 11831cb0ef41Sopenharmony_cilistener array. If any single listener has been added multiple times to the 11841cb0ef41Sopenharmony_cilistener array for the specified <code>eventName</code>, then <code>removeListener()</code> must be 11851cb0ef41Sopenharmony_cicalled multiple times to remove each instance.</p> 11861cb0ef41Sopenharmony_ci<p>Once an event is emitted, all listeners attached to it at the 11871cb0ef41Sopenharmony_citime of emitting are called in order. This implies that any 11881cb0ef41Sopenharmony_ci<code>removeListener()</code> or <code>removeAllListeners()</code> calls <em>after</em> emitting and 11891cb0ef41Sopenharmony_ci<em>before</em> the last listener finishes execution will not remove them from 11901cb0ef41Sopenharmony_ci<code>emit()</code> in progress. Subsequent events behave as expected.</p> 11911cb0ef41Sopenharmony_ci 11921cb0ef41Sopenharmony_ci<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>; 11931cb0ef41Sopenharmony_ci<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> {} 11941cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 11951cb0ef41Sopenharmony_ci 11961cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> <span class="hljs-title function_">callbackA</span> = (<span class="hljs-params"></span>) => { 11971cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'A'</span>); 11981cb0ef41Sopenharmony_ci myEmitter.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'event'</span>, callbackB); 11991cb0ef41Sopenharmony_ci}; 12001cb0ef41Sopenharmony_ci 12011cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> <span class="hljs-title function_">callbackB</span> = (<span class="hljs-params"></span>) => { 12021cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'B'</span>); 12031cb0ef41Sopenharmony_ci}; 12041cb0ef41Sopenharmony_ci 12051cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, callbackA); 12061cb0ef41Sopenharmony_ci 12071cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, callbackB); 12081cb0ef41Sopenharmony_ci 12091cb0ef41Sopenharmony_ci<span class="hljs-comment">// callbackA removes listener callbackB but it will still be called.</span> 12101cb0ef41Sopenharmony_ci<span class="hljs-comment">// Internal listener array at time of emit [callbackA, callbackB]</span> 12111cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 12121cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 12131cb0ef41Sopenharmony_ci<span class="hljs-comment">// A</span> 12141cb0ef41Sopenharmony_ci<span class="hljs-comment">// B</span> 12151cb0ef41Sopenharmony_ci 12161cb0ef41Sopenharmony_ci<span class="hljs-comment">// callbackB is now removed.</span> 12171cb0ef41Sopenharmony_ci<span class="hljs-comment">// Internal listener array [callbackA]</span> 12181cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 12191cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 12201cb0ef41Sopenharmony_ci<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>); 12211cb0ef41Sopenharmony_ci<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> {} 12221cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyEmitter</span>(); 12231cb0ef41Sopenharmony_ci 12241cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> <span class="hljs-title function_">callbackA</span> = (<span class="hljs-params"></span>) => { 12251cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'A'</span>); 12261cb0ef41Sopenharmony_ci myEmitter.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'event'</span>, callbackB); 12271cb0ef41Sopenharmony_ci}; 12281cb0ef41Sopenharmony_ci 12291cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> <span class="hljs-title function_">callbackB</span> = (<span class="hljs-params"></span>) => { 12301cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'B'</span>); 12311cb0ef41Sopenharmony_ci}; 12321cb0ef41Sopenharmony_ci 12331cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, callbackA); 12341cb0ef41Sopenharmony_ci 12351cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, callbackB); 12361cb0ef41Sopenharmony_ci 12371cb0ef41Sopenharmony_ci<span class="hljs-comment">// callbackA removes listener callbackB but it will still be called.</span> 12381cb0ef41Sopenharmony_ci<span class="hljs-comment">// Internal listener array at time of emit [callbackA, callbackB]</span> 12391cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 12401cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 12411cb0ef41Sopenharmony_ci<span class="hljs-comment">// A</span> 12421cb0ef41Sopenharmony_ci<span class="hljs-comment">// B</span> 12431cb0ef41Sopenharmony_ci 12441cb0ef41Sopenharmony_ci<span class="hljs-comment">// callbackB is now removed.</span> 12451cb0ef41Sopenharmony_ci<span class="hljs-comment">// Internal listener array [callbackA]</span> 12461cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'event'</span>); 12471cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints:</span> 12481cb0ef41Sopenharmony_ci<span class="hljs-comment">// A</span></code><button class="copy-button">copy</button></pre> 12491cb0ef41Sopenharmony_ci<p>Because listeners are managed using an internal array, calling this will 12501cb0ef41Sopenharmony_cichange the position indices of any listener registered <em>after</em> the listener 12511cb0ef41Sopenharmony_cibeing removed. This will not impact the order in which listeners are called, 12521cb0ef41Sopenharmony_cibut it means that any copies of the listener array as returned by 12531cb0ef41Sopenharmony_cithe <code>emitter.listeners()</code> method will need to be recreated.</p> 12541cb0ef41Sopenharmony_ci<p>When a single function has been added as a handler multiple times for a single 12551cb0ef41Sopenharmony_cievent (as in the example below), <code>removeListener()</code> will remove the most 12561cb0ef41Sopenharmony_cirecently added instance. In the example the <code>once('ping')</code> 12571cb0ef41Sopenharmony_cilistener is removed:</p> 12581cb0ef41Sopenharmony_ci 12591cb0ef41Sopenharmony_ci<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>; 12601cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 12611cb0ef41Sopenharmony_ci 12621cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">pong</span>(<span class="hljs-params"></span>) { 12631cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'pong'</span>); 12641cb0ef41Sopenharmony_ci} 12651cb0ef41Sopenharmony_ci 12661cb0ef41Sopenharmony_ciee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'ping'</span>, pong); 12671cb0ef41Sopenharmony_ciee.<span class="hljs-title function_">once</span>(<span class="hljs-string">'ping'</span>, pong); 12681cb0ef41Sopenharmony_ciee.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'ping'</span>, pong); 12691cb0ef41Sopenharmony_ci 12701cb0ef41Sopenharmony_ciee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'ping'</span>); 12711cb0ef41Sopenharmony_ciee.<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>); 12721cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 12731cb0ef41Sopenharmony_ci 12741cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">pong</span>(<span class="hljs-params"></span>) { 12751cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'pong'</span>); 12761cb0ef41Sopenharmony_ci} 12771cb0ef41Sopenharmony_ci 12781cb0ef41Sopenharmony_ciee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'ping'</span>, pong); 12791cb0ef41Sopenharmony_ciee.<span class="hljs-title function_">once</span>(<span class="hljs-string">'ping'</span>, pong); 12801cb0ef41Sopenharmony_ciee.<span class="hljs-title function_">removeListener</span>(<span class="hljs-string">'ping'</span>, pong); 12811cb0ef41Sopenharmony_ci 12821cb0ef41Sopenharmony_ciee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'ping'</span>); 12831cb0ef41Sopenharmony_ciee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'ping'</span>);</code><button class="copy-button">copy</button></pre> 12841cb0ef41Sopenharmony_ci<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p> 12851cb0ef41Sopenharmony_ci<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> 12861cb0ef41Sopenharmony_ci<div class="api_metadata"> 12871cb0ef41Sopenharmony_ci<span>Added in: v0.3.5</span> 12881cb0ef41Sopenharmony_ci</div> 12891cb0ef41Sopenharmony_ci<ul> 12901cb0ef41Sopenharmony_ci<li><code>n</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 12911cb0ef41Sopenharmony_ci<li>Returns: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 12921cb0ef41Sopenharmony_ci</ul> 12931cb0ef41Sopenharmony_ci<p>By default <code>EventEmitter</code>s will print a warning if more than <code>10</code> listeners are 12941cb0ef41Sopenharmony_ciadded for a particular event. This is a useful default that helps finding 12951cb0ef41Sopenharmony_cimemory leaks. The <code>emitter.setMaxListeners()</code> method allows the limit to be 12961cb0ef41Sopenharmony_cimodified for this specific <code>EventEmitter</code> instance. The value can be set to 12971cb0ef41Sopenharmony_ci<code>Infinity</code> (or <code>0</code>) to indicate an unlimited number of listeners.</p> 12981cb0ef41Sopenharmony_ci<p>Returns a reference to the <code>EventEmitter</code>, so that calls can be chained.</p> 12991cb0ef41Sopenharmony_ci<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> 13001cb0ef41Sopenharmony_ci<div class="api_metadata"> 13011cb0ef41Sopenharmony_ci<span>Added in: v9.4.0</span> 13021cb0ef41Sopenharmony_ci</div> 13031cb0ef41Sopenharmony_ci<ul> 13041cb0ef41Sopenharmony_ci<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> 13051cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function[]></a></li> 13061cb0ef41Sopenharmony_ci</ul> 13071cb0ef41Sopenharmony_ci<p>Returns a copy of the array of listeners for the event named <code>eventName</code>, 13081cb0ef41Sopenharmony_ciincluding any wrappers (such as those created by <code>.once()</code>).</p> 13091cb0ef41Sopenharmony_ci 13101cb0ef41Sopenharmony_ci<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>; 13111cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> emitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 13121cb0ef41Sopenharmony_ciemitter.<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>)); 13131cb0ef41Sopenharmony_ci 13141cb0ef41Sopenharmony_ci<span class="hljs-comment">// Returns a new Array with a function `onceWrapper` which has a property</span> 13151cb0ef41Sopenharmony_ci<span class="hljs-comment">// `listener` which contains the original listener bound above</span> 13161cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> listeners = emitter.<span class="hljs-title function_">rawListeners</span>(<span class="hljs-string">'log'</span>); 13171cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> logFnWrapper = listeners[<span class="hljs-number">0</span>]; 13181cb0ef41Sopenharmony_ci 13191cb0ef41Sopenharmony_ci<span class="hljs-comment">// Logs "log once" to the console and does not unbind the `once` event</span> 13201cb0ef41Sopenharmony_cilogFnWrapper.<span class="hljs-title function_">listener</span>(); 13211cb0ef41Sopenharmony_ci 13221cb0ef41Sopenharmony_ci<span class="hljs-comment">// Logs "log once" to the console and removes the listener</span> 13231cb0ef41Sopenharmony_ci<span class="hljs-title function_">logFnWrapper</span>(); 13241cb0ef41Sopenharmony_ci 13251cb0ef41Sopenharmony_ciemitter.<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>)); 13261cb0ef41Sopenharmony_ci<span class="hljs-comment">// Will return a new Array with a single function bound by `.on()` above</span> 13271cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> newListeners = emitter.<span class="hljs-title function_">rawListeners</span>(<span class="hljs-string">'log'</span>); 13281cb0ef41Sopenharmony_ci 13291cb0ef41Sopenharmony_ci<span class="hljs-comment">// Logs "log persistently" twice</span> 13301cb0ef41Sopenharmony_cinewListeners[<span class="hljs-number">0</span>](); 13311cb0ef41Sopenharmony_ciemitter.<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>); 13321cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> emitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 13331cb0ef41Sopenharmony_ciemitter.<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>)); 13341cb0ef41Sopenharmony_ci 13351cb0ef41Sopenharmony_ci<span class="hljs-comment">// Returns a new Array with a function `onceWrapper` which has a property</span> 13361cb0ef41Sopenharmony_ci<span class="hljs-comment">// `listener` which contains the original listener bound above</span> 13371cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> listeners = emitter.<span class="hljs-title function_">rawListeners</span>(<span class="hljs-string">'log'</span>); 13381cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> logFnWrapper = listeners[<span class="hljs-number">0</span>]; 13391cb0ef41Sopenharmony_ci 13401cb0ef41Sopenharmony_ci<span class="hljs-comment">// Logs "log once" to the console and does not unbind the `once` event</span> 13411cb0ef41Sopenharmony_cilogFnWrapper.<span class="hljs-title function_">listener</span>(); 13421cb0ef41Sopenharmony_ci 13431cb0ef41Sopenharmony_ci<span class="hljs-comment">// Logs "log once" to the console and removes the listener</span> 13441cb0ef41Sopenharmony_ci<span class="hljs-title function_">logFnWrapper</span>(); 13451cb0ef41Sopenharmony_ci 13461cb0ef41Sopenharmony_ciemitter.<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>)); 13471cb0ef41Sopenharmony_ci<span class="hljs-comment">// Will return a new Array with a single function bound by `.on()` above</span> 13481cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> newListeners = emitter.<span class="hljs-title function_">rawListeners</span>(<span class="hljs-string">'log'</span>); 13491cb0ef41Sopenharmony_ci 13501cb0ef41Sopenharmony_ci<span class="hljs-comment">// Logs "log persistently" twice</span> 13511cb0ef41Sopenharmony_cinewListeners[<span class="hljs-number">0</span>](); 13521cb0ef41Sopenharmony_ciemitter.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'log'</span>);</code><button class="copy-button">copy</button></pre> 13531cb0ef41Sopenharmony_ci<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> 13541cb0ef41Sopenharmony_ci<div class="api_metadata"> 13551cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 13561cb0ef41Sopenharmony_ci<table> 13571cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 13581cb0ef41Sopenharmony_ci<tr><td>v17.4.0, v16.14.0</td> 13591cb0ef41Sopenharmony_ci<td><p>No longer experimental.</p></td></tr> 13601cb0ef41Sopenharmony_ci<tr><td>v13.4.0, v12.16.0</td> 13611cb0ef41Sopenharmony_ci<td><p><span>Added in: v13.4.0, v12.16.0</span></p></td></tr> 13621cb0ef41Sopenharmony_ci</tbody></table> 13631cb0ef41Sopenharmony_ci</details> 13641cb0ef41Sopenharmony_ci</div> 13651cb0ef41Sopenharmony_ci<ul> 13661cb0ef41Sopenharmony_ci<li><code>err</code> Error</li> 13671cb0ef41Sopenharmony_ci<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> 13681cb0ef41Sopenharmony_ci<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 13691cb0ef41Sopenharmony_ci</ul> 13701cb0ef41Sopenharmony_ci<p>The <code>Symbol.for('nodejs.rejection')</code> method is called in case a 13711cb0ef41Sopenharmony_cipromise rejection happens when emitting an event and 13721cb0ef41Sopenharmony_ci<a href="#capture-rejections-of-promises"><code>captureRejections</code></a> is enabled on the emitter. 13731cb0ef41Sopenharmony_ciIt is possible to use <a href="#eventscapturerejectionsymbol"><code>events.captureRejectionSymbol</code></a> in 13741cb0ef41Sopenharmony_ciplace of <code>Symbol.for('nodejs.rejection')</code>.</p> 13751cb0ef41Sopenharmony_ci 13761cb0ef41Sopenharmony_ci<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>; 13771cb0ef41Sopenharmony_ci 13781cb0ef41Sopenharmony_ci<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> { 13791cb0ef41Sopenharmony_ci <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) { 13801cb0ef41Sopenharmony_ci <span class="hljs-variable language_">super</span>({ <span class="hljs-attr">captureRejections</span>: <span class="hljs-literal">true</span> }); 13811cb0ef41Sopenharmony_ci } 13821cb0ef41Sopenharmony_ci 13831cb0ef41Sopenharmony_ci [captureRejectionSymbol](err, event, ...args) { 13841cb0ef41Sopenharmony_ci <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); 13851cb0ef41Sopenharmony_ci <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">destroy</span>(err); 13861cb0ef41Sopenharmony_ci } 13871cb0ef41Sopenharmony_ci 13881cb0ef41Sopenharmony_ci <span class="hljs-title function_">destroy</span>(<span class="hljs-params">err</span>) { 13891cb0ef41Sopenharmony_ci <span class="hljs-comment">// Tear the resource down here.</span> 13901cb0ef41Sopenharmony_ci } 13911cb0ef41Sopenharmony_ci}</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>); 13921cb0ef41Sopenharmony_ci 13931cb0ef41Sopenharmony_ci<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> { 13941cb0ef41Sopenharmony_ci <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) { 13951cb0ef41Sopenharmony_ci <span class="hljs-variable language_">super</span>({ <span class="hljs-attr">captureRejections</span>: <span class="hljs-literal">true</span> }); 13961cb0ef41Sopenharmony_ci } 13971cb0ef41Sopenharmony_ci 13981cb0ef41Sopenharmony_ci [captureRejectionSymbol](err, event, ...args) { 13991cb0ef41Sopenharmony_ci <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); 14001cb0ef41Sopenharmony_ci <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">destroy</span>(err); 14011cb0ef41Sopenharmony_ci } 14021cb0ef41Sopenharmony_ci 14031cb0ef41Sopenharmony_ci <span class="hljs-title function_">destroy</span>(<span class="hljs-params">err</span>) { 14041cb0ef41Sopenharmony_ci <span class="hljs-comment">// Tear the resource down here.</span> 14051cb0ef41Sopenharmony_ci } 14061cb0ef41Sopenharmony_ci}</code><button class="copy-button">copy</button></pre> 14071cb0ef41Sopenharmony_ci</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> 14081cb0ef41Sopenharmony_ci<div class="api_metadata"> 14091cb0ef41Sopenharmony_ci<span>Added in: v0.11.2</span> 14101cb0ef41Sopenharmony_ci</div> 14111cb0ef41Sopenharmony_ci<p>By default, a maximum of <code>10</code> listeners can be registered for any single 14121cb0ef41Sopenharmony_cievent. This limit can be changed for individual <code>EventEmitter</code> instances 14131cb0ef41Sopenharmony_ciusing the <a href="#emittersetmaxlistenersn"><code>emitter.setMaxListeners(n)</code></a> method. To change the default 14141cb0ef41Sopenharmony_cifor <em>all</em> <code>EventEmitter</code> instances, the <code>events.defaultMaxListeners</code> 14151cb0ef41Sopenharmony_ciproperty can be used. If this value is not a positive number, a <code>RangeError</code> 14161cb0ef41Sopenharmony_ciis thrown.</p> 14171cb0ef41Sopenharmony_ci<p>Take caution when setting the <code>events.defaultMaxListeners</code> because the 14181cb0ef41Sopenharmony_cichange affects <em>all</em> <code>EventEmitter</code> instances, including those created before 14191cb0ef41Sopenharmony_cithe change is made. However, calling <a href="#emittersetmaxlistenersn"><code>emitter.setMaxListeners(n)</code></a> still has 14201cb0ef41Sopenharmony_ciprecedence over <code>events.defaultMaxListeners</code>.</p> 14211cb0ef41Sopenharmony_ci<p>This is not a hard limit. The <code>EventEmitter</code> instance will allow 14221cb0ef41Sopenharmony_cimore listeners to be added but will output a trace warning to stderr indicating 14231cb0ef41Sopenharmony_cithat a "possible EventEmitter memory leak" has been detected. For any single 14241cb0ef41Sopenharmony_ci<code>EventEmitter</code>, the <code>emitter.getMaxListeners()</code> and <code>emitter.setMaxListeners()</code> 14251cb0ef41Sopenharmony_cimethods can be used to temporarily avoid this warning:</p> 14261cb0ef41Sopenharmony_ci 14271cb0ef41Sopenharmony_ci<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>; 14281cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> emitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 14291cb0ef41Sopenharmony_ciemitter.<span class="hljs-title function_">setMaxListeners</span>(emitter.<span class="hljs-title function_">getMaxListeners</span>() + <span class="hljs-number">1</span>); 14301cb0ef41Sopenharmony_ciemitter.<span class="hljs-title function_">once</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 14311cb0ef41Sopenharmony_ci <span class="hljs-comment">// do stuff</span> 14321cb0ef41Sopenharmony_ci 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>)); 14331cb0ef41Sopenharmony_ci});</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>); 14341cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> emitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 14351cb0ef41Sopenharmony_ciemitter.<span class="hljs-title function_">setMaxListeners</span>(emitter.<span class="hljs-title function_">getMaxListeners</span>() + <span class="hljs-number">1</span>); 14361cb0ef41Sopenharmony_ciemitter.<span class="hljs-title function_">once</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> { 14371cb0ef41Sopenharmony_ci <span class="hljs-comment">// do stuff</span> 14381cb0ef41Sopenharmony_ci 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>)); 14391cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 14401cb0ef41Sopenharmony_ci<p>The <a href="cli.html#--trace-warnings"><code>--trace-warnings</code></a> command-line flag can be used to display the 14411cb0ef41Sopenharmony_cistack trace for such warnings.</p> 14421cb0ef41Sopenharmony_ci<p>The emitted warning can be inspected with <a href="process.html#event-warning"><code>process.on('warning')</code></a> and will 14431cb0ef41Sopenharmony_cihave the additional <code>emitter</code>, <code>type</code>, and <code>count</code> properties, referring to 14441cb0ef41Sopenharmony_cithe event emitter instance, the event's name and the number of attached 14451cb0ef41Sopenharmony_cilisteners, respectively. 14461cb0ef41Sopenharmony_ciIts <code>name</code> property is set to <code>'MaxListenersExceededWarning'</code>.</p> 14471cb0ef41Sopenharmony_ci</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> 14481cb0ef41Sopenharmony_ci<div class="api_metadata"> 14491cb0ef41Sopenharmony_ci<span>Added in: v13.6.0, v12.17.0</span> 14501cb0ef41Sopenharmony_ci</div> 14511cb0ef41Sopenharmony_ci<p>This symbol shall be used to install a listener for only monitoring <code>'error'</code> 14521cb0ef41Sopenharmony_cievents. Listeners installed using this symbol are called before the regular 14531cb0ef41Sopenharmony_ci<code>'error'</code> listeners are called.</p> 14541cb0ef41Sopenharmony_ci<p>Installing a listener using this symbol does not change the behavior once an 14551cb0ef41Sopenharmony_ci<code>'error'</code> event is emitted. Therefore, the process will still crash if no 14561cb0ef41Sopenharmony_ciregular <code>'error'</code> listener is installed.</p> 14571cb0ef41Sopenharmony_ci</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> 14581cb0ef41Sopenharmony_ci<div class="api_metadata"> 14591cb0ef41Sopenharmony_ci<span>Added in: v15.2.0, v14.17.0</span> 14601cb0ef41Sopenharmony_ci</div> 14611cb0ef41Sopenharmony_ci<ul> 14621cb0ef41Sopenharmony_ci<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> 14631cb0ef41Sopenharmony_ci<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> 14641cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function[]></a></li> 14651cb0ef41Sopenharmony_ci</ul> 14661cb0ef41Sopenharmony_ci<p>Returns a copy of the array of listeners for the event named <code>eventName</code>.</p> 14671cb0ef41Sopenharmony_ci<p>For <code>EventEmitter</code>s this behaves exactly the same as calling <code>.listeners</code> on 14681cb0ef41Sopenharmony_cithe emitter.</p> 14691cb0ef41Sopenharmony_ci<p>For <code>EventTarget</code>s this is the only way to get the event listeners for the 14701cb0ef41Sopenharmony_cievent target. This is useful for debugging and diagnostic purposes.</p> 14711cb0ef41Sopenharmony_ci 14721cb0ef41Sopenharmony_ci<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>; 14731cb0ef41Sopenharmony_ci 14741cb0ef41Sopenharmony_ci{ 14751cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 14761cb0ef41Sopenharmony_ci <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>); 14771cb0ef41Sopenharmony_ci ee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, listener); 14781cb0ef41Sopenharmony_ci <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> 14791cb0ef41Sopenharmony_ci} 14801cb0ef41Sopenharmony_ci{ 14811cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> et = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventTarget</span>(); 14821cb0ef41Sopenharmony_ci <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>); 14831cb0ef41Sopenharmony_ci et.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, listener); 14841cb0ef41Sopenharmony_ci <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> 14851cb0ef41Sopenharmony_ci}</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>); 14861cb0ef41Sopenharmony_ci 14871cb0ef41Sopenharmony_ci{ 14881cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 14891cb0ef41Sopenharmony_ci <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>); 14901cb0ef41Sopenharmony_ci ee.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, listener); 14911cb0ef41Sopenharmony_ci <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> 14921cb0ef41Sopenharmony_ci} 14931cb0ef41Sopenharmony_ci{ 14941cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> et = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventTarget</span>(); 14951cb0ef41Sopenharmony_ci <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>); 14961cb0ef41Sopenharmony_ci et.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, listener); 14971cb0ef41Sopenharmony_ci <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> 14981cb0ef41Sopenharmony_ci}</code><button class="copy-button">copy</button></pre> 14991cb0ef41Sopenharmony_ci</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> 15001cb0ef41Sopenharmony_ci<div class="api_metadata"> 15011cb0ef41Sopenharmony_ci<span>Added in: v18.17.0</span> 15021cb0ef41Sopenharmony_ci</div> 15031cb0ef41Sopenharmony_ci<ul> 15041cb0ef41Sopenharmony_ci<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> 15051cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 15061cb0ef41Sopenharmony_ci</ul> 15071cb0ef41Sopenharmony_ci<p>Returns the currently set max amount of listeners.</p> 15081cb0ef41Sopenharmony_ci<p>For <code>EventEmitter</code>s this behaves exactly the same as calling <code>.getMaxListeners</code> on 15091cb0ef41Sopenharmony_cithe emitter.</p> 15101cb0ef41Sopenharmony_ci<p>For <code>EventTarget</code>s this is the only way to get the max event listeners for the 15111cb0ef41Sopenharmony_cievent target. If the number of event handlers on a single EventTarget exceeds 15121cb0ef41Sopenharmony_cithe max set, the EventTarget will print a warning.</p> 15131cb0ef41Sopenharmony_ci 15141cb0ef41Sopenharmony_ci<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>; 15151cb0ef41Sopenharmony_ci 15161cb0ef41Sopenharmony_ci{ 15171cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 15181cb0ef41Sopenharmony_ci <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> 15191cb0ef41Sopenharmony_ci <span class="hljs-title function_">setMaxListeners</span>(<span class="hljs-number">11</span>, ee); 15201cb0ef41Sopenharmony_ci <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> 15211cb0ef41Sopenharmony_ci} 15221cb0ef41Sopenharmony_ci{ 15231cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> et = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventTarget</span>(); 15241cb0ef41Sopenharmony_ci <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> 15251cb0ef41Sopenharmony_ci <span class="hljs-title function_">setMaxListeners</span>(<span class="hljs-number">11</span>, et); 15261cb0ef41Sopenharmony_ci <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> 15271cb0ef41Sopenharmony_ci}</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>); 15281cb0ef41Sopenharmony_ci 15291cb0ef41Sopenharmony_ci{ 15301cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 15311cb0ef41Sopenharmony_ci <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> 15321cb0ef41Sopenharmony_ci <span class="hljs-title function_">setMaxListeners</span>(<span class="hljs-number">11</span>, ee); 15331cb0ef41Sopenharmony_ci <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> 15341cb0ef41Sopenharmony_ci} 15351cb0ef41Sopenharmony_ci{ 15361cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> et = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventTarget</span>(); 15371cb0ef41Sopenharmony_ci <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> 15381cb0ef41Sopenharmony_ci <span class="hljs-title function_">setMaxListeners</span>(<span class="hljs-number">11</span>, et); 15391cb0ef41Sopenharmony_ci <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> 15401cb0ef41Sopenharmony_ci}</code><button class="copy-button">copy</button></pre> 15411cb0ef41Sopenharmony_ci</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> 15421cb0ef41Sopenharmony_ci<div class="api_metadata"> 15431cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 15441cb0ef41Sopenharmony_ci<table> 15451cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 15461cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 15471cb0ef41Sopenharmony_ci<td><p>The <code>signal</code> option is supported now.</p></td></tr> 15481cb0ef41Sopenharmony_ci<tr><td>v11.13.0, v10.16.0</td> 15491cb0ef41Sopenharmony_ci<td><p><span>Added in: v11.13.0, v10.16.0</span></p></td></tr> 15501cb0ef41Sopenharmony_ci</tbody></table> 15511cb0ef41Sopenharmony_ci</details> 15521cb0ef41Sopenharmony_ci</div> 15531cb0ef41Sopenharmony_ci<ul> 15541cb0ef41Sopenharmony_ci<li><code>emitter</code> <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 15551cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 15561cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 15571cb0ef41Sopenharmony_ci<ul> 15581cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Can be used to cancel waiting for the event.</li> 15591cb0ef41Sopenharmony_ci</ul> 15601cb0ef41Sopenharmony_ci</li> 15611cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 15621cb0ef41Sopenharmony_ci</ul> 15631cb0ef41Sopenharmony_ci<p>Creates a <code>Promise</code> that is fulfilled when the <code>EventEmitter</code> emits the given 15641cb0ef41Sopenharmony_cievent or that is rejected if the <code>EventEmitter</code> emits <code>'error'</code> while waiting. 15651cb0ef41Sopenharmony_ciThe <code>Promise</code> will resolve with an array of all the arguments emitted to the 15661cb0ef41Sopenharmony_cigiven event.</p> 15671cb0ef41Sopenharmony_ci<p>This method is intentionally generic and works with the web platform 15681cb0ef41Sopenharmony_ci<a href="https://dom.spec.whatwg.org/#interface-eventtarget">EventTarget</a> interface, which has no special 15691cb0ef41Sopenharmony_ci<code>'error'</code> event semantics and does not listen to the <code>'error'</code> event.</p> 15701cb0ef41Sopenharmony_ci 15711cb0ef41Sopenharmony_ci<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>; 15721cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 15731cb0ef41Sopenharmony_ci 15741cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 15751cb0ef41Sopenharmony_ci 15761cb0ef41Sopenharmony_ciprocess.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 15771cb0ef41Sopenharmony_ci ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'myevent'</span>, <span class="hljs-number">42</span>); 15781cb0ef41Sopenharmony_ci}); 15791cb0ef41Sopenharmony_ci 15801cb0ef41Sopenharmony_ci<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>); 15811cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); 15821cb0ef41Sopenharmony_ci 15831cb0ef41Sopenharmony_ci<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>); 15841cb0ef41Sopenharmony_ciprocess.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 15851cb0ef41Sopenharmony_ci ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'error'</span>, err); 15861cb0ef41Sopenharmony_ci}); 15871cb0ef41Sopenharmony_ci 15881cb0ef41Sopenharmony_ci<span class="hljs-keyword">try</span> { 15891cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(ee, <span class="hljs-string">'myevent'</span>); 15901cb0ef41Sopenharmony_ci} <span class="hljs-keyword">catch</span> (err) { 15911cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'error happened'</span>, err); 15921cb0ef41Sopenharmony_ci}</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>); 15931cb0ef41Sopenharmony_ci 15941cb0ef41Sopenharmony_ci<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">run</span>(<span class="hljs-params"></span>) { 15951cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 15961cb0ef41Sopenharmony_ci 15971cb0ef41Sopenharmony_ci process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 15981cb0ef41Sopenharmony_ci ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'myevent'</span>, <span class="hljs-number">42</span>); 15991cb0ef41Sopenharmony_ci }); 16001cb0ef41Sopenharmony_ci 16011cb0ef41Sopenharmony_ci <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>); 16021cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); 16031cb0ef41Sopenharmony_ci 16041cb0ef41Sopenharmony_ci <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>); 16051cb0ef41Sopenharmony_ci process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 16061cb0ef41Sopenharmony_ci ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'error'</span>, err); 16071cb0ef41Sopenharmony_ci }); 16081cb0ef41Sopenharmony_ci 16091cb0ef41Sopenharmony_ci <span class="hljs-keyword">try</span> { 16101cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(ee, <span class="hljs-string">'myevent'</span>); 16111cb0ef41Sopenharmony_ci } <span class="hljs-keyword">catch</span> (err) { 16121cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'error happened'</span>, err); 16131cb0ef41Sopenharmony_ci } 16141cb0ef41Sopenharmony_ci} 16151cb0ef41Sopenharmony_ci 16161cb0ef41Sopenharmony_ci<span class="hljs-title function_">run</span>();</code><button class="copy-button">copy</button></pre> 16171cb0ef41Sopenharmony_ci<p>The special handling of the <code>'error'</code> event is only used when <code>events.once()</code> 16181cb0ef41Sopenharmony_ciis used to wait for another event. If <code>events.once()</code> is used to wait for the 16191cb0ef41Sopenharmony_ci'<code>error'</code> event itself, then it is treated as any other kind of event without 16201cb0ef41Sopenharmony_cispecial handling:</p> 16211cb0ef41Sopenharmony_ci 16221cb0ef41Sopenharmony_ci<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>; 16231cb0ef41Sopenharmony_ci 16241cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 16251cb0ef41Sopenharmony_ci 16261cb0ef41Sopenharmony_ci<span class="hljs-title function_">once</span>(ee, <span class="hljs-string">'error'</span>) 16271cb0ef41Sopenharmony_ci .<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>)) 16281cb0ef41Sopenharmony_ci .<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>)); 16291cb0ef41Sopenharmony_ci 16301cb0ef41Sopenharmony_ciee.<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>)); 16311cb0ef41Sopenharmony_ci 16321cb0ef41Sopenharmony_ci<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>); 16331cb0ef41Sopenharmony_ci 16341cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 16351cb0ef41Sopenharmony_ci 16361cb0ef41Sopenharmony_ci<span class="hljs-title function_">once</span>(ee, <span class="hljs-string">'error'</span>) 16371cb0ef41Sopenharmony_ci .<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>)) 16381cb0ef41Sopenharmony_ci .<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>)); 16391cb0ef41Sopenharmony_ci 16401cb0ef41Sopenharmony_ciee.<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>)); 16411cb0ef41Sopenharmony_ci 16421cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: ok boom</span></code><button class="copy-button">copy</button></pre> 16431cb0ef41Sopenharmony_ci<p>An <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> can be used to cancel waiting for the event:</p> 16441cb0ef41Sopenharmony_ci 16451cb0ef41Sopenharmony_ci<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>; 16461cb0ef41Sopenharmony_ci 16471cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 16481cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 16491cb0ef41Sopenharmony_ci 16501cb0ef41Sopenharmony_ci<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>) { 16511cb0ef41Sopenharmony_ci <span class="hljs-keyword">try</span> { 16521cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(emitter, event, { signal }); 16531cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'event emitted!'</span>); 16541cb0ef41Sopenharmony_ci } <span class="hljs-keyword">catch</span> (error) { 16551cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (error.<span class="hljs-property">name</span> === <span class="hljs-string">'AbortError'</span>) { 16561cb0ef41Sopenharmony_ci <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>); 16571cb0ef41Sopenharmony_ci } <span class="hljs-keyword">else</span> { 16581cb0ef41Sopenharmony_ci <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>); 16591cb0ef41Sopenharmony_ci } 16601cb0ef41Sopenharmony_ci } 16611cb0ef41Sopenharmony_ci} 16621cb0ef41Sopenharmony_ci 16631cb0ef41Sopenharmony_ci<span class="hljs-title function_">foo</span>(ee, <span class="hljs-string">'foo'</span>, ac.<span class="hljs-property">signal</span>); 16641cb0ef41Sopenharmony_ciac.<span class="hljs-title function_">abort</span>(); <span class="hljs-comment">// Abort waiting for the event</span> 16651cb0ef41Sopenharmony_ciee.<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>); 16661cb0ef41Sopenharmony_ci 16671cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 16681cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 16691cb0ef41Sopenharmony_ci 16701cb0ef41Sopenharmony_ci<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>) { 16711cb0ef41Sopenharmony_ci <span class="hljs-keyword">try</span> { 16721cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(emitter, event, { signal }); 16731cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'event emitted!'</span>); 16741cb0ef41Sopenharmony_ci } <span class="hljs-keyword">catch</span> (error) { 16751cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (error.<span class="hljs-property">name</span> === <span class="hljs-string">'AbortError'</span>) { 16761cb0ef41Sopenharmony_ci <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>); 16771cb0ef41Sopenharmony_ci } <span class="hljs-keyword">else</span> { 16781cb0ef41Sopenharmony_ci <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>); 16791cb0ef41Sopenharmony_ci } 16801cb0ef41Sopenharmony_ci } 16811cb0ef41Sopenharmony_ci} 16821cb0ef41Sopenharmony_ci 16831cb0ef41Sopenharmony_ci<span class="hljs-title function_">foo</span>(ee, <span class="hljs-string">'foo'</span>, ac.<span class="hljs-property">signal</span>); 16841cb0ef41Sopenharmony_ciac.<span class="hljs-title function_">abort</span>(); <span class="hljs-comment">// Abort waiting for the event</span> 16851cb0ef41Sopenharmony_ciee.<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> 16861cb0ef41Sopenharmony_ci<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> 16871cb0ef41Sopenharmony_ci<p>There is an edge case worth noting when using the <code>events.once()</code> function 16881cb0ef41Sopenharmony_cito await multiple events emitted on in the same batch of <code>process.nextTick()</code> 16891cb0ef41Sopenharmony_cioperations, or whenever multiple events are emitted synchronously. Specifically, 16901cb0ef41Sopenharmony_cibecause the <code>process.nextTick()</code> queue is drained before the <code>Promise</code> microtask 16911cb0ef41Sopenharmony_ciqueue, and because <code>EventEmitter</code> emits all events synchronously, it is possible 16921cb0ef41Sopenharmony_cifor <code>events.once()</code> to miss an event.</p> 16931cb0ef41Sopenharmony_ci 16941cb0ef41Sopenharmony_ci<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>; 16951cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 16961cb0ef41Sopenharmony_ci 16971cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 16981cb0ef41Sopenharmony_ci 16991cb0ef41Sopenharmony_ci<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) { 17001cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(myEE, <span class="hljs-string">'bar'</span>); 17011cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'bar'</span>); 17021cb0ef41Sopenharmony_ci 17031cb0ef41Sopenharmony_ci <span class="hljs-comment">// This Promise will never resolve because the 'foo' event will</span> 17041cb0ef41Sopenharmony_ci <span class="hljs-comment">// have already been emitted before the Promise is created.</span> 17051cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(myEE, <span class="hljs-string">'foo'</span>); 17061cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'foo'</span>); 17071cb0ef41Sopenharmony_ci} 17081cb0ef41Sopenharmony_ci 17091cb0ef41Sopenharmony_ciprocess.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 17101cb0ef41Sopenharmony_ci myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'bar'</span>); 17111cb0ef41Sopenharmony_ci myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 17121cb0ef41Sopenharmony_ci}); 17131cb0ef41Sopenharmony_ci 17141cb0ef41Sopenharmony_ci<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>); 17151cb0ef41Sopenharmony_ci 17161cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 17171cb0ef41Sopenharmony_ci 17181cb0ef41Sopenharmony_ci<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) { 17191cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(myEE, <span class="hljs-string">'bar'</span>); 17201cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'bar'</span>); 17211cb0ef41Sopenharmony_ci 17221cb0ef41Sopenharmony_ci <span class="hljs-comment">// This Promise will never resolve because the 'foo' event will</span> 17231cb0ef41Sopenharmony_ci <span class="hljs-comment">// have already been emitted before the Promise is created.</span> 17241cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(myEE, <span class="hljs-string">'foo'</span>); 17251cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'foo'</span>); 17261cb0ef41Sopenharmony_ci} 17271cb0ef41Sopenharmony_ci 17281cb0ef41Sopenharmony_ciprocess.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 17291cb0ef41Sopenharmony_ci myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'bar'</span>); 17301cb0ef41Sopenharmony_ci myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 17311cb0ef41Sopenharmony_ci}); 17321cb0ef41Sopenharmony_ci 17331cb0ef41Sopenharmony_ci<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> 17341cb0ef41Sopenharmony_ci<p>To catch both events, create each of the Promises <em>before</em> awaiting either 17351cb0ef41Sopenharmony_ciof them, then it becomes possible to use <code>Promise.all()</code>, <code>Promise.race()</code>, 17361cb0ef41Sopenharmony_cior <code>Promise.allSettled()</code>:</p> 17371cb0ef41Sopenharmony_ci 17381cb0ef41Sopenharmony_ci<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>; 17391cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 17401cb0ef41Sopenharmony_ci 17411cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 17421cb0ef41Sopenharmony_ci 17431cb0ef41Sopenharmony_ci<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) { 17441cb0ef41Sopenharmony_ci <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>)]); 17451cb0ef41Sopenharmony_ci <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>); 17461cb0ef41Sopenharmony_ci} 17471cb0ef41Sopenharmony_ci 17481cb0ef41Sopenharmony_ciprocess.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 17491cb0ef41Sopenharmony_ci myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'bar'</span>); 17501cb0ef41Sopenharmony_ci myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 17511cb0ef41Sopenharmony_ci}); 17521cb0ef41Sopenharmony_ci 17531cb0ef41Sopenharmony_ci<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>); 17541cb0ef41Sopenharmony_ci 17551cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEE = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 17561cb0ef41Sopenharmony_ci 17571cb0ef41Sopenharmony_ci<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) { 17581cb0ef41Sopenharmony_ci <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>)]); 17591cb0ef41Sopenharmony_ci <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>); 17601cb0ef41Sopenharmony_ci} 17611cb0ef41Sopenharmony_ci 17621cb0ef41Sopenharmony_ciprocess.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 17631cb0ef41Sopenharmony_ci myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'bar'</span>); 17641cb0ef41Sopenharmony_ci myEE.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 17651cb0ef41Sopenharmony_ci}); 17661cb0ef41Sopenharmony_ci 17671cb0ef41Sopenharmony_ci<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> 17681cb0ef41Sopenharmony_ci</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> 17691cb0ef41Sopenharmony_ci<div class="api_metadata"> 17701cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 17711cb0ef41Sopenharmony_ci<table> 17721cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 17731cb0ef41Sopenharmony_ci<tr><td>v17.4.0, v16.14.0</td> 17741cb0ef41Sopenharmony_ci<td><p>No longer experimental.</p></td></tr> 17751cb0ef41Sopenharmony_ci<tr><td>v13.4.0, v12.16.0</td> 17761cb0ef41Sopenharmony_ci<td><p><span>Added in: v13.4.0, v12.16.0</span></p></td></tr> 17771cb0ef41Sopenharmony_ci</tbody></table> 17781cb0ef41Sopenharmony_ci</details> 17791cb0ef41Sopenharmony_ci</div> 17801cb0ef41Sopenharmony_ci<p>Value: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></p> 17811cb0ef41Sopenharmony_ci<p>Change the default <code>captureRejections</code> option on all new <code>EventEmitter</code> objects.</p> 17821cb0ef41Sopenharmony_ci</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> 17831cb0ef41Sopenharmony_ci<div class="api_metadata"> 17841cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 17851cb0ef41Sopenharmony_ci<table> 17861cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 17871cb0ef41Sopenharmony_ci<tr><td>v17.4.0, v16.14.0</td> 17881cb0ef41Sopenharmony_ci<td><p>No longer experimental.</p></td></tr> 17891cb0ef41Sopenharmony_ci<tr><td>v13.4.0, v12.16.0</td> 17901cb0ef41Sopenharmony_ci<td><p><span>Added in: v13.4.0, v12.16.0</span></p></td></tr> 17911cb0ef41Sopenharmony_ci</tbody></table> 17921cb0ef41Sopenharmony_ci</details> 17931cb0ef41Sopenharmony_ci</div> 17941cb0ef41Sopenharmony_ci<p>Value: <code>Symbol.for('nodejs.rejection')</code></p> 17951cb0ef41Sopenharmony_ci<p>See how to write a custom <a href="#emittersymbolfornodejsrejectionerr-eventname-args">rejection handler</a>.</p> 17961cb0ef41Sopenharmony_ci</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> 17971cb0ef41Sopenharmony_ci<div class="api_metadata"> 17981cb0ef41Sopenharmony_ci<span>Added in: v0.9.12</span><span>Deprecated since: v3.2.0</span> 17991cb0ef41Sopenharmony_ci</div> 18001cb0ef41Sopenharmony_ci<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> 18011cb0ef41Sopenharmony_ci<ul> 18021cb0ef41Sopenharmony_ci<li><code>emitter</code> <a href="events.html#class-eventemitter" class="type"><EventEmitter></a> The emitter to query</li> 18031cb0ef41Sopenharmony_ci<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> 18041cb0ef41Sopenharmony_ci</ul> 18051cb0ef41Sopenharmony_ci<p>A class method that returns the number of listeners for the given <code>eventName</code> 18061cb0ef41Sopenharmony_ciregistered on the given <code>emitter</code>.</p> 18071cb0ef41Sopenharmony_ci 18081cb0ef41Sopenharmony_ci<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>; 18091cb0ef41Sopenharmony_ci 18101cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 18111cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> {}); 18121cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> {}); 18131cb0ef41Sopenharmony_ci<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>)); 18141cb0ef41Sopenharmony_ci<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>); 18151cb0ef41Sopenharmony_ci 18161cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> myEmitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 18171cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> {}); 18181cb0ef41Sopenharmony_cimyEmitter.<span class="hljs-title function_">on</span>(<span class="hljs-string">'event'</span>, <span class="hljs-function">() =></span> {}); 18191cb0ef41Sopenharmony_ci<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>)); 18201cb0ef41Sopenharmony_ci<span class="hljs-comment">// Prints: 2</span></code><button class="copy-button">copy</button></pre> 18211cb0ef41Sopenharmony_ci</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> 18221cb0ef41Sopenharmony_ci<div class="api_metadata"> 18231cb0ef41Sopenharmony_ci<span>Added in: v13.6.0, v12.16.0</span> 18241cb0ef41Sopenharmony_ci</div> 18251cb0ef41Sopenharmony_ci<ul> 18261cb0ef41Sopenharmony_ci<li><code>emitter</code> <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 18271cb0ef41Sopenharmony_ci<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> 18281cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 18291cb0ef41Sopenharmony_ci<ul> 18301cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Can be used to cancel awaiting events.</li> 18311cb0ef41Sopenharmony_ci</ul> 18321cb0ef41Sopenharmony_ci</li> 18331cb0ef41Sopenharmony_ci<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> 18341cb0ef41Sopenharmony_ci</ul> 18351cb0ef41Sopenharmony_ci 18361cb0ef41Sopenharmony_ci<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>; 18371cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 18381cb0ef41Sopenharmony_ci 18391cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 18401cb0ef41Sopenharmony_ci 18411cb0ef41Sopenharmony_ci<span class="hljs-comment">// Emit later on</span> 18421cb0ef41Sopenharmony_ciprocess.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 18431cb0ef41Sopenharmony_ci ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'bar'</span>); 18441cb0ef41Sopenharmony_ci ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-number">42</span>); 18451cb0ef41Sopenharmony_ci}); 18461cb0ef41Sopenharmony_ci 18471cb0ef41Sopenharmony_ci<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>)) { 18481cb0ef41Sopenharmony_ci <span class="hljs-comment">// The execution of this inner block is synchronous and it</span> 18491cb0ef41Sopenharmony_ci <span class="hljs-comment">// processes one event at a time (even with await). Do not use</span> 18501cb0ef41Sopenharmony_ci <span class="hljs-comment">// if concurrent execution is required.</span> 18511cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event); <span class="hljs-comment">// prints ['bar'] [42]</span> 18521cb0ef41Sopenharmony_ci} 18531cb0ef41Sopenharmony_ci<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>); 18541cb0ef41Sopenharmony_ci 18551cb0ef41Sopenharmony_ci(<span class="hljs-keyword">async</span> () => { 18561cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 18571cb0ef41Sopenharmony_ci 18581cb0ef41Sopenharmony_ci <span class="hljs-comment">// Emit later on</span> 18591cb0ef41Sopenharmony_ci process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 18601cb0ef41Sopenharmony_ci ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'bar'</span>); 18611cb0ef41Sopenharmony_ci ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-number">42</span>); 18621cb0ef41Sopenharmony_ci }); 18631cb0ef41Sopenharmony_ci 18641cb0ef41Sopenharmony_ci <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>)) { 18651cb0ef41Sopenharmony_ci <span class="hljs-comment">// The execution of this inner block is synchronous and it</span> 18661cb0ef41Sopenharmony_ci <span class="hljs-comment">// processes one event at a time (even with await). Do not use</span> 18671cb0ef41Sopenharmony_ci <span class="hljs-comment">// if concurrent execution is required.</span> 18681cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event); <span class="hljs-comment">// prints ['bar'] [42]</span> 18691cb0ef41Sopenharmony_ci } 18701cb0ef41Sopenharmony_ci <span class="hljs-comment">// Unreachable here</span> 18711cb0ef41Sopenharmony_ci})();</code><button class="copy-button">copy</button></pre> 18721cb0ef41Sopenharmony_ci<p>Returns an <code>AsyncIterator</code> that iterates <code>eventName</code> events. It will throw 18731cb0ef41Sopenharmony_ciif the <code>EventEmitter</code> emits <code>'error'</code>. It removes all listeners when 18741cb0ef41Sopenharmony_ciexiting the loop. The <code>value</code> returned by each iteration is an array 18751cb0ef41Sopenharmony_cicomposed of the emitted event arguments.</p> 18761cb0ef41Sopenharmony_ci<p>An <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> can be used to cancel waiting on events:</p> 18771cb0ef41Sopenharmony_ci 18781cb0ef41Sopenharmony_ci<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>; 18791cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 18801cb0ef41Sopenharmony_ci 18811cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 18821cb0ef41Sopenharmony_ci 18831cb0ef41Sopenharmony_ci(<span class="hljs-keyword">async</span> () => { 18841cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 18851cb0ef41Sopenharmony_ci 18861cb0ef41Sopenharmony_ci <span class="hljs-comment">// Emit later on</span> 18871cb0ef41Sopenharmony_ci process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 18881cb0ef41Sopenharmony_ci ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'bar'</span>); 18891cb0ef41Sopenharmony_ci ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-number">42</span>); 18901cb0ef41Sopenharmony_ci }); 18911cb0ef41Sopenharmony_ci 18921cb0ef41Sopenharmony_ci <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> })) { 18931cb0ef41Sopenharmony_ci <span class="hljs-comment">// The execution of this inner block is synchronous and it</span> 18941cb0ef41Sopenharmony_ci <span class="hljs-comment">// processes one event at a time (even with await). Do not use</span> 18951cb0ef41Sopenharmony_ci <span class="hljs-comment">// if concurrent execution is required.</span> 18961cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event); <span class="hljs-comment">// prints ['bar'] [42]</span> 18971cb0ef41Sopenharmony_ci } 18981cb0ef41Sopenharmony_ci <span class="hljs-comment">// Unreachable here</span> 18991cb0ef41Sopenharmony_ci})(); 19001cb0ef41Sopenharmony_ci 19011cb0ef41Sopenharmony_ciprocess.<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>); 19021cb0ef41Sopenharmony_ci 19031cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 19041cb0ef41Sopenharmony_ci 19051cb0ef41Sopenharmony_ci(<span class="hljs-keyword">async</span> () => { 19061cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> ee = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 19071cb0ef41Sopenharmony_ci 19081cb0ef41Sopenharmony_ci <span class="hljs-comment">// Emit later on</span> 19091cb0ef41Sopenharmony_ci process.<span class="hljs-title function_">nextTick</span>(<span class="hljs-function">() =></span> { 19101cb0ef41Sopenharmony_ci ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-string">'bar'</span>); 19111cb0ef41Sopenharmony_ci ee.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-number">42</span>); 19121cb0ef41Sopenharmony_ci }); 19131cb0ef41Sopenharmony_ci 19141cb0ef41Sopenharmony_ci <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> })) { 19151cb0ef41Sopenharmony_ci <span class="hljs-comment">// The execution of this inner block is synchronous and it</span> 19161cb0ef41Sopenharmony_ci <span class="hljs-comment">// processes one event at a time (even with await). Do not use</span> 19171cb0ef41Sopenharmony_ci <span class="hljs-comment">// if concurrent execution is required.</span> 19181cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event); <span class="hljs-comment">// prints ['bar'] [42]</span> 19191cb0ef41Sopenharmony_ci } 19201cb0ef41Sopenharmony_ci <span class="hljs-comment">// Unreachable here</span> 19211cb0ef41Sopenharmony_ci})(); 19221cb0ef41Sopenharmony_ci 19231cb0ef41Sopenharmony_ciprocess.<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> 19241cb0ef41Sopenharmony_ci</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> 19251cb0ef41Sopenharmony_ci<div class="api_metadata"> 19261cb0ef41Sopenharmony_ci<span>Added in: v15.4.0</span> 19271cb0ef41Sopenharmony_ci</div> 19281cb0ef41Sopenharmony_ci<ul> 19291cb0ef41Sopenharmony_ci<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 19301cb0ef41Sopenharmony_ci<code>EventTarget</code> event.</li> 19311cb0ef41Sopenharmony_ci<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> 19321cb0ef41Sopenharmony_cior <a href="events.html#class-eventemitter" class="type"><EventEmitter></a> instances. If none are specified, <code>n</code> is set as the default 19331cb0ef41Sopenharmony_cimax 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> 19341cb0ef41Sopenharmony_ci</ul> 19351cb0ef41Sopenharmony_ci 19361cb0ef41Sopenharmony_ci<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>; 19371cb0ef41Sopenharmony_ci 19381cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> target = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventTarget</span>(); 19391cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> emitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 19401cb0ef41Sopenharmony_ci 19411cb0ef41Sopenharmony_ci<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> { 19421cb0ef41Sopenharmony_ci setMaxListeners, 19431cb0ef41Sopenharmony_ci <span class="hljs-title class_">EventEmitter</span>, 19441cb0ef41Sopenharmony_ci} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 19451cb0ef41Sopenharmony_ci 19461cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> target = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventTarget</span>(); 19471cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> emitter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 19481cb0ef41Sopenharmony_ci 19491cb0ef41Sopenharmony_ci<span class="hljs-title function_">setMaxListeners</span>(<span class="hljs-number">5</span>, target, emitter);</code><button class="copy-button">copy</button></pre> 19501cb0ef41Sopenharmony_ci</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> 19511cb0ef41Sopenharmony_ci<div class="api_metadata"> 19521cb0ef41Sopenharmony_ci<span>Added in: v18.18.0</span> 19531cb0ef41Sopenharmony_ci</div> 19541cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 19551cb0ef41Sopenharmony_ci<ul> 19561cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a></li> 19571cb0ef41Sopenharmony_ci<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> 19581cb0ef41Sopenharmony_ci<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> 19591cb0ef41Sopenharmony_ci</ul> 19601cb0ef41Sopenharmony_ci<p>Listens once to the <code>abort</code> event on the provided <code>signal</code>.</p> 19611cb0ef41Sopenharmony_ci<p>Listening to the <code>abort</code> event on abort signals is unsafe and may 19621cb0ef41Sopenharmony_cilead to resource leaks since another third party with the signal can 19631cb0ef41Sopenharmony_cicall <a href="#eventstopimmediatepropagation"><code>e.stopImmediatePropagation()</code></a>. Unfortunately Node.js cannot change 19641cb0ef41Sopenharmony_cithis since it would violate the web standard. Additionally, the original 19651cb0ef41Sopenharmony_ciAPI makes it easy to forget to remove listeners.</p> 19661cb0ef41Sopenharmony_ci<p>This API allows safely using <code>AbortSignal</code>s in Node.js APIs by solving these 19671cb0ef41Sopenharmony_citwo issues by listening to the event such that <code>stopImmediatePropagation</code> does 19681cb0ef41Sopenharmony_cinot prevent the listener from running.</p> 19691cb0ef41Sopenharmony_ci<p>Returns a disposable so that it may be unsubscribed from more easily.</p> 19701cb0ef41Sopenharmony_ci 19711cb0ef41Sopenharmony_ci<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>); 19721cb0ef41Sopenharmony_ci 19731cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">example</span>(<span class="hljs-params">signal</span>) { 19741cb0ef41Sopenharmony_ci <span class="hljs-keyword">let</span> disposable; 19751cb0ef41Sopenharmony_ci <span class="hljs-keyword">try</span> { 19761cb0ef41Sopenharmony_ci 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>()); 19771cb0ef41Sopenharmony_ci disposable = <span class="hljs-title function_">addAbortListener</span>(signal, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 19781cb0ef41Sopenharmony_ci <span class="hljs-comment">// Do something when signal is aborted.</span> 19791cb0ef41Sopenharmony_ci }); 19801cb0ef41Sopenharmony_ci } <span class="hljs-keyword">finally</span> { 19811cb0ef41Sopenharmony_ci disposable?.[<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">dispose</span>](); 19821cb0ef41Sopenharmony_ci } 19831cb0ef41Sopenharmony_ci}</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>; 19841cb0ef41Sopenharmony_ci 19851cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">example</span>(<span class="hljs-params">signal</span>) { 19861cb0ef41Sopenharmony_ci <span class="hljs-keyword">let</span> disposable; 19871cb0ef41Sopenharmony_ci <span class="hljs-keyword">try</span> { 19881cb0ef41Sopenharmony_ci 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>()); 19891cb0ef41Sopenharmony_ci disposable = <span class="hljs-title function_">addAbortListener</span>(signal, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 19901cb0ef41Sopenharmony_ci <span class="hljs-comment">// Do something when signal is aborted.</span> 19911cb0ef41Sopenharmony_ci }); 19921cb0ef41Sopenharmony_ci } <span class="hljs-keyword">finally</span> { 19931cb0ef41Sopenharmony_ci disposable?.[<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">dispose</span>](); 19941cb0ef41Sopenharmony_ci } 19951cb0ef41Sopenharmony_ci}</code><button class="copy-button">copy</button></pre> 19961cb0ef41Sopenharmony_ci</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> 19971cb0ef41Sopenharmony_ci<div class="api_metadata"> 19981cb0ef41Sopenharmony_ci<span>Added in: v17.4.0, v16.14.0</span> 19991cb0ef41Sopenharmony_ci</div> 20001cb0ef41Sopenharmony_ci<p>Integrates <code>EventEmitter</code> with <a href="async_hooks.html#class-asyncresource" class="type"><AsyncResource></a> for <code>EventEmitter</code>s that 20011cb0ef41Sopenharmony_cirequire manual async tracking. Specifically, all events emitted by instances 20021cb0ef41Sopenharmony_ciof <code>events.EventEmitterAsyncResource</code> will run within its <a href="async_context.html">async context</a>.</p> 20031cb0ef41Sopenharmony_ci 20041cb0ef41Sopenharmony_ci<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>; 20051cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { notStrictEqual, strictEqual } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:assert'</span>; 20061cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { executionAsyncId, triggerAsyncId } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:async_hooks'</span>; 20071cb0ef41Sopenharmony_ci 20081cb0ef41Sopenharmony_ci<span class="hljs-comment">// Async tracking tooling will identify this as 'Q'.</span> 20091cb0ef41Sopenharmony_ci<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> }); 20101cb0ef41Sopenharmony_ci 20111cb0ef41Sopenharmony_ci<span class="hljs-comment">// 'foo' listeners will run in the EventEmitters async context.</span> 20121cb0ef41Sopenharmony_ciee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">() =></span> { 20131cb0ef41Sopenharmony_ci <span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">executionAsyncId</span>(), ee1.<span class="hljs-property">asyncId</span>); 20141cb0ef41Sopenharmony_ci <span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">triggerAsyncId</span>(), ee1.<span class="hljs-property">triggerAsyncId</span>); 20151cb0ef41Sopenharmony_ci}); 20161cb0ef41Sopenharmony_ci 20171cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ee2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 20181cb0ef41Sopenharmony_ci 20191cb0ef41Sopenharmony_ci<span class="hljs-comment">// 'foo' listeners on ordinary EventEmitters that do not track async</span> 20201cb0ef41Sopenharmony_ci<span class="hljs-comment">// context, however, run in the same async context as the emit().</span> 20211cb0ef41Sopenharmony_ciee2.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">() =></span> { 20221cb0ef41Sopenharmony_ci <span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-title function_">executionAsyncId</span>(), ee2.<span class="hljs-property">asyncId</span>); 20231cb0ef41Sopenharmony_ci <span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-title function_">triggerAsyncId</span>(), ee2.<span class="hljs-property">triggerAsyncId</span>); 20241cb0ef41Sopenharmony_ci}); 20251cb0ef41Sopenharmony_ci 20261cb0ef41Sopenharmony_ci<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> { 20271cb0ef41Sopenharmony_ci ee1.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 20281cb0ef41Sopenharmony_ci ee2.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 20291cb0ef41Sopenharmony_ci});</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>); 20301cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { notStrictEqual, strictEqual } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:assert'</span>); 20311cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { executionAsyncId, triggerAsyncId } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:async_hooks'</span>); 20321cb0ef41Sopenharmony_ci 20331cb0ef41Sopenharmony_ci<span class="hljs-comment">// Async tracking tooling will identify this as 'Q'.</span> 20341cb0ef41Sopenharmony_ci<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> }); 20351cb0ef41Sopenharmony_ci 20361cb0ef41Sopenharmony_ci<span class="hljs-comment">// 'foo' listeners will run in the EventEmitters async context.</span> 20371cb0ef41Sopenharmony_ciee1.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">() =></span> { 20381cb0ef41Sopenharmony_ci <span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">executionAsyncId</span>(), ee1.<span class="hljs-property">asyncId</span>); 20391cb0ef41Sopenharmony_ci <span class="hljs-title function_">strictEqual</span>(<span class="hljs-title function_">triggerAsyncId</span>(), ee1.<span class="hljs-property">triggerAsyncId</span>); 20401cb0ef41Sopenharmony_ci}); 20411cb0ef41Sopenharmony_ci 20421cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> ee2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventEmitter</span>(); 20431cb0ef41Sopenharmony_ci 20441cb0ef41Sopenharmony_ci<span class="hljs-comment">// 'foo' listeners on ordinary EventEmitters that do not track async</span> 20451cb0ef41Sopenharmony_ci<span class="hljs-comment">// context, however, run in the same async context as the emit().</span> 20461cb0ef41Sopenharmony_ciee2.<span class="hljs-title function_">on</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">() =></span> { 20471cb0ef41Sopenharmony_ci <span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-title function_">executionAsyncId</span>(), ee2.<span class="hljs-property">asyncId</span>); 20481cb0ef41Sopenharmony_ci <span class="hljs-title function_">notStrictEqual</span>(<span class="hljs-title function_">triggerAsyncId</span>(), ee2.<span class="hljs-property">triggerAsyncId</span>); 20491cb0ef41Sopenharmony_ci}); 20501cb0ef41Sopenharmony_ci 20511cb0ef41Sopenharmony_ci<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> { 20521cb0ef41Sopenharmony_ci ee1.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 20531cb0ef41Sopenharmony_ci ee2.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'foo'</span>); 20541cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 20551cb0ef41Sopenharmony_ci<p>The <code>EventEmitterAsyncResource</code> class has the same methods and takes the 20561cb0ef41Sopenharmony_cisame options as <code>EventEmitter</code> and <code>AsyncResource</code> themselves.</p> 20571cb0ef41Sopenharmony_ci<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> 20581cb0ef41Sopenharmony_ci<ul> 20591cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 20601cb0ef41Sopenharmony_ci<ul> 20611cb0ef41Sopenharmony_ci<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 20621cb0ef41Sopenharmony_ci<a href="#capture-rejections-of-promises">automatic capturing of promise rejection</a>. 20631cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li> 20641cb0ef41Sopenharmony_ci<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> 20651cb0ef41Sopenharmony_ci<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 20661cb0ef41Sopenharmony_ciasync event. <strong>Default:</strong> <code>executionAsyncId()</code>.</li> 20671cb0ef41Sopenharmony_ci<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> 20681cb0ef41Sopenharmony_ciwhen the object is garbage collected. This usually does not need to be set 20691cb0ef41Sopenharmony_ci(even if <code>emitDestroy</code> is called manually), unless the resource's <code>asyncId</code> 20701cb0ef41Sopenharmony_ciis retrieved and the sensitive API's <code>emitDestroy</code> is called with it. 20711cb0ef41Sopenharmony_ciWhen set to <code>false</code>, the <code>emitDestroy</code> call on garbage collection 20721cb0ef41Sopenharmony_ciwill only take place if there is at least one active <code>destroy</code> hook. 20731cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li> 20741cb0ef41Sopenharmony_ci</ul> 20751cb0ef41Sopenharmony_ci</li> 20761cb0ef41Sopenharmony_ci</ul> 20771cb0ef41Sopenharmony_ci<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> 20781cb0ef41Sopenharmony_ci<ul> 20791cb0ef41Sopenharmony_ci<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> 20801cb0ef41Sopenharmony_ci</ul> 20811cb0ef41Sopenharmony_ci<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> 20821cb0ef41Sopenharmony_ci<ul> 20831cb0ef41Sopenharmony_ci<li>Type: The underlying <a href="async_hooks.html#class-asyncresource" class="type"><AsyncResource></a>.</li> 20841cb0ef41Sopenharmony_ci</ul> 20851cb0ef41Sopenharmony_ci<p>The returned <code>AsyncResource</code> object has an additional <code>eventEmitter</code> property 20861cb0ef41Sopenharmony_cithat provides a reference to this <code>EventEmitterAsyncResource</code>.</p> 20871cb0ef41Sopenharmony_ci<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> 20881cb0ef41Sopenharmony_ci<p>Call all <code>destroy</code> hooks. This should only ever be called once. An error will 20891cb0ef41Sopenharmony_cibe thrown if it is called more than once. This <strong>must</strong> be manually called. If 20901cb0ef41Sopenharmony_cithe resource is left to be collected by the GC then the <code>destroy</code> hooks will 20911cb0ef41Sopenharmony_cinever be called.</p> 20921cb0ef41Sopenharmony_ci<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> 20931cb0ef41Sopenharmony_ci<ul> 20941cb0ef41Sopenharmony_ci<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 20951cb0ef41Sopenharmony_ci<code>AsyncResource</code> constructor.</li> 20961cb0ef41Sopenharmony_ci</ul> 20971cb0ef41Sopenharmony_ci<p><a id="event-target-and-event-api"></a></p> 20981cb0ef41Sopenharmony_ci</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> 20991cb0ef41Sopenharmony_ci<div class="api_metadata"> 21001cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 21011cb0ef41Sopenharmony_ci<table> 21021cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 21031cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td> 21041cb0ef41Sopenharmony_ci<td><p>changed EventTarget error handling.</p></td></tr> 21051cb0ef41Sopenharmony_ci<tr><td>v15.4.0</td> 21061cb0ef41Sopenharmony_ci<td><p>No longer experimental.</p></td></tr> 21071cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 21081cb0ef41Sopenharmony_ci<td><p>The <code>EventTarget</code> and <code>Event</code> classes are now available as globals.</p></td></tr> 21091cb0ef41Sopenharmony_ci<tr><td>v14.5.0</td> 21101cb0ef41Sopenharmony_ci<td><p><span>Added in: v14.5.0</span></p></td></tr> 21111cb0ef41Sopenharmony_ci</tbody></table> 21121cb0ef41Sopenharmony_ci</details> 21131cb0ef41Sopenharmony_ci</div> 21141cb0ef41Sopenharmony_ci<p>The <code>EventTarget</code> and <code>Event</code> objects are a Node.js-specific implementation 21151cb0ef41Sopenharmony_ciof 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> 21161cb0ef41Sopenharmony_ci<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>(); 21171cb0ef41Sopenharmony_ci 21181cb0ef41Sopenharmony_citarget.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, <span class="hljs-function">(<span class="hljs-params">event</span>) =></span> { 21191cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'foo event happened!'</span>); 21201cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 21211cb0ef41Sopenharmony_ci<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> 21221cb0ef41Sopenharmony_ci<p>There are two key differences between the Node.js <code>EventTarget</code> and the 21231cb0ef41Sopenharmony_ci<a href="https://dom.spec.whatwg.org/#eventtarget"><code>EventTarget</code> Web API</a>:</p> 21241cb0ef41Sopenharmony_ci<ol> 21251cb0ef41Sopenharmony_ci<li>Whereas DOM <code>EventTarget</code> instances <em>may</em> be hierarchical, there is no 21261cb0ef41Sopenharmony_ciconcept of hierarchy and event propagation in Node.js. That is, an event 21271cb0ef41Sopenharmony_cidispatched to an <code>EventTarget</code> does not propagate through a hierarchy of 21281cb0ef41Sopenharmony_cinested target objects that may each have their own set of handlers for the 21291cb0ef41Sopenharmony_cievent.</li> 21301cb0ef41Sopenharmony_ci<li>In the Node.js <code>EventTarget</code>, if an event listener is an async function 21311cb0ef41Sopenharmony_cior returns a <code>Promise</code>, and the returned <code>Promise</code> rejects, the rejection 21321cb0ef41Sopenharmony_ciis automatically captured and handled the same way as a listener that 21331cb0ef41Sopenharmony_cithrows synchronously (see <a href="#eventtarget-error-handling"><code>EventTarget</code> error handling</a> for details).</li> 21341cb0ef41Sopenharmony_ci</ol> 21351cb0ef41Sopenharmony_ci<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> 21361cb0ef41Sopenharmony_ci<p>The <code>NodeEventTarget</code> object implements a modified subset of the 21371cb0ef41Sopenharmony_ci<code>EventEmitter</code> API that allows it to closely <em>emulate</em> an <code>EventEmitter</code> in 21381cb0ef41Sopenharmony_cicertain situations. A <code>NodeEventTarget</code> is <em>not</em> an instance of <code>EventEmitter</code> 21391cb0ef41Sopenharmony_ciand cannot be used in place of an <code>EventEmitter</code> in most cases.</p> 21401cb0ef41Sopenharmony_ci<ol> 21411cb0ef41Sopenharmony_ci<li>Unlike <code>EventEmitter</code>, any given <code>listener</code> can be registered at most once 21421cb0ef41Sopenharmony_ciper event <code>type</code>. Attempts to register a <code>listener</code> multiple times are 21431cb0ef41Sopenharmony_ciignored.</li> 21441cb0ef41Sopenharmony_ci<li>The <code>NodeEventTarget</code> does not emulate the full <code>EventEmitter</code> API. 21451cb0ef41Sopenharmony_ciSpecifically the <code>prependListener()</code>, <code>prependOnceListener()</code>, 21461cb0ef41Sopenharmony_ci<code>rawListeners()</code>, and <code>errorMonitor</code> APIs are not emulated. 21471cb0ef41Sopenharmony_ciThe <code>'newListener'</code> and <code>'removeListener'</code> events will also not be emitted.</li> 21481cb0ef41Sopenharmony_ci<li>The <code>NodeEventTarget</code> does not implement any special default behavior 21491cb0ef41Sopenharmony_cifor events with type <code>'error'</code>.</li> 21501cb0ef41Sopenharmony_ci<li>The <code>NodeEventTarget</code> supports <code>EventListener</code> objects as well as 21511cb0ef41Sopenharmony_cifunctions as handlers for all event types.</li> 21521cb0ef41Sopenharmony_ci</ol> 21531cb0ef41Sopenharmony_ci<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> 21541cb0ef41Sopenharmony_ci<p>Event listeners registered for an event <code>type</code> may either be JavaScript 21551cb0ef41Sopenharmony_cifunctions or objects with a <code>handleEvent</code> property whose value is a function.</p> 21561cb0ef41Sopenharmony_ci<p>In either case, the handler function is invoked with the <code>event</code> argument 21571cb0ef41Sopenharmony_cipassed to the <code>eventTarget.dispatchEvent()</code> function.</p> 21581cb0ef41Sopenharmony_ci<p>Async functions may be used as event listeners. If an async handler function 21591cb0ef41Sopenharmony_cirejects, the rejection is captured and handled as described in 21601cb0ef41Sopenharmony_ci<a href="#eventtarget-error-handling"><code>EventTarget</code> error handling</a>.</p> 21611cb0ef41Sopenharmony_ci<p>An error thrown by one handler function does not prevent the other handlers 21621cb0ef41Sopenharmony_cifrom being invoked.</p> 21631cb0ef41Sopenharmony_ci<p>The return value of a handler function is ignored.</p> 21641cb0ef41Sopenharmony_ci<p>Handlers are always invoked in the order they were added.</p> 21651cb0ef41Sopenharmony_ci<p>Handler functions may mutate the <code>event</code> object.</p> 21661cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">handler1</span>(<span class="hljs-params">event</span>) { 21671cb0ef41Sopenharmony_ci <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> 21681cb0ef41Sopenharmony_ci event.<span class="hljs-property">a</span> = <span class="hljs-number">1</span>; 21691cb0ef41Sopenharmony_ci} 21701cb0ef41Sopenharmony_ci 21711cb0ef41Sopenharmony_ci<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>) { 21721cb0ef41Sopenharmony_ci <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> 21731cb0ef41Sopenharmony_ci <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> 21741cb0ef41Sopenharmony_ci} 21751cb0ef41Sopenharmony_ci 21761cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> handler3 = { 21771cb0ef41Sopenharmony_ci <span class="hljs-title function_">handleEvent</span>(<span class="hljs-params">event</span>) { 21781cb0ef41Sopenharmony_ci <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> 21791cb0ef41Sopenharmony_ci }, 21801cb0ef41Sopenharmony_ci}; 21811cb0ef41Sopenharmony_ci 21821cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> handler4 = { 21831cb0ef41Sopenharmony_ci <span class="hljs-keyword">async</span> <span class="hljs-title function_">handleEvent</span>(<span class="hljs-params">event</span>) { 21841cb0ef41Sopenharmony_ci <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> 21851cb0ef41Sopenharmony_ci }, 21861cb0ef41Sopenharmony_ci}; 21871cb0ef41Sopenharmony_ci 21881cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> target = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventTarget</span>(); 21891cb0ef41Sopenharmony_ci 21901cb0ef41Sopenharmony_citarget.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, handler1); 21911cb0ef41Sopenharmony_citarget.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, handler2); 21921cb0ef41Sopenharmony_citarget.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'foo'</span>, handler3); 21931cb0ef41Sopenharmony_citarget.<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> 21941cb0ef41Sopenharmony_ci<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> 21951cb0ef41Sopenharmony_ci<p>When a registered event listener throws (or returns a Promise that rejects), 21961cb0ef41Sopenharmony_ciby default the error is treated as an uncaught exception on 21971cb0ef41Sopenharmony_ci<code>process.nextTick()</code>. This means uncaught exceptions in <code>EventTarget</code>s will 21981cb0ef41Sopenharmony_citerminate the Node.js process by default.</p> 21991cb0ef41Sopenharmony_ci<p>Throwing within an event listener will <em>not</em> stop the other registered handlers 22001cb0ef41Sopenharmony_cifrom being invoked.</p> 22011cb0ef41Sopenharmony_ci<p>The <code>EventTarget</code> does not implement any special default handling for <code>'error'</code> 22021cb0ef41Sopenharmony_citype events like <code>EventEmitter</code>.</p> 22031cb0ef41Sopenharmony_ci<p>Currently errors are first forwarded to the <code>process.on('error')</code> event 22041cb0ef41Sopenharmony_cibefore reaching <code>process.on('uncaughtException')</code>. This behavior is 22051cb0ef41Sopenharmony_cideprecated and will change in a future release to align <code>EventTarget</code> with 22061cb0ef41Sopenharmony_ciother Node.js APIs. Any code relying on the <code>process.on('error')</code> event should 22071cb0ef41Sopenharmony_cibe aligned with the new behavior.</p> 22081cb0ef41Sopenharmony_ci<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> 22091cb0ef41Sopenharmony_ci<div class="api_metadata"> 22101cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 22111cb0ef41Sopenharmony_ci<table> 22121cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 22131cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 22141cb0ef41Sopenharmony_ci<td><p>The <code>Event</code> class is now available through the global object.</p></td></tr> 22151cb0ef41Sopenharmony_ci<tr><td>v14.5.0</td> 22161cb0ef41Sopenharmony_ci<td><p><span>Added in: v14.5.0</span></p></td></tr> 22171cb0ef41Sopenharmony_ci</tbody></table> 22181cb0ef41Sopenharmony_ci</details> 22191cb0ef41Sopenharmony_ci</div> 22201cb0ef41Sopenharmony_ci<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 22211cb0ef41Sopenharmony_ciare created internally by Node.js.</p> 22221cb0ef41Sopenharmony_ci<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> 22231cb0ef41Sopenharmony_ci<div class="api_metadata"> 22241cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 22251cb0ef41Sopenharmony_ci</div> 22261cb0ef41Sopenharmony_ci<ul> 22271cb0ef41Sopenharmony_ci<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> 22281cb0ef41Sopenharmony_ci</ul> 22291cb0ef41Sopenharmony_ci<p>This is not used in Node.js and is provided purely for completeness.</p> 22301cb0ef41Sopenharmony_ci<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> 22311cb0ef41Sopenharmony_ci<div class="api_metadata"> 22321cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 22331cb0ef41Sopenharmony_ci</div> 22341cb0ef41Sopenharmony_ci<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> 22351cb0ef41Sopenharmony_ci<ul> 22361cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 22371cb0ef41Sopenharmony_ci</ul> 22381cb0ef41Sopenharmony_ci<p>Alias for <code>event.stopPropagation()</code> if set to <code>true</code>. This is not used 22391cb0ef41Sopenharmony_ciin Node.js and is provided purely for completeness.</p> 22401cb0ef41Sopenharmony_ci<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> 22411cb0ef41Sopenharmony_ci<div class="api_metadata"> 22421cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 22431cb0ef41Sopenharmony_ci</div> 22441cb0ef41Sopenharmony_ci<ul> 22451cb0ef41Sopenharmony_ci<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> 22461cb0ef41Sopenharmony_ci</ul> 22471cb0ef41Sopenharmony_ci<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> 22481cb0ef41Sopenharmony_ci<div class="api_metadata"> 22491cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 22501cb0ef41Sopenharmony_ci</div> 22511cb0ef41Sopenharmony_ci<ul> 22521cb0ef41Sopenharmony_ci<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> 22531cb0ef41Sopenharmony_ci</ul> 22541cb0ef41Sopenharmony_ci<p>This is not used in Node.js and is provided purely for completeness.</p> 22551cb0ef41Sopenharmony_ci<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> 22561cb0ef41Sopenharmony_ci<div class="api_metadata"> 22571cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 22581cb0ef41Sopenharmony_ci</div> 22591cb0ef41Sopenharmony_ci<p>Returns an array containing the current <code>EventTarget</code> as the only entry or 22601cb0ef41Sopenharmony_ciempty if the event is not being dispatched. This is not used in 22611cb0ef41Sopenharmony_ciNode.js and is provided purely for completeness.</p> 22621cb0ef41Sopenharmony_ci<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> 22631cb0ef41Sopenharmony_ci<div class="api_metadata"> 22641cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 22651cb0ef41Sopenharmony_ci</div> 22661cb0ef41Sopenharmony_ci<ul> 22671cb0ef41Sopenharmony_ci<li>Type: <a href="events.html#class-eventtarget" class="type"><EventTarget></a> The <code>EventTarget</code> dispatching the event.</li> 22681cb0ef41Sopenharmony_ci</ul> 22691cb0ef41Sopenharmony_ci<p>Alias for <code>event.target</code>.</p> 22701cb0ef41Sopenharmony_ci<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> 22711cb0ef41Sopenharmony_ci<div class="api_metadata"> 22721cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 22731cb0ef41Sopenharmony_ci</div> 22741cb0ef41Sopenharmony_ci<ul> 22751cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 22761cb0ef41Sopenharmony_ci</ul> 22771cb0ef41Sopenharmony_ci<p>Is <code>true</code> if <code>cancelable</code> is <code>true</code> and <code>event.preventDefault()</code> has been 22781cb0ef41Sopenharmony_cicalled.</p> 22791cb0ef41Sopenharmony_ci<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> 22801cb0ef41Sopenharmony_ci<div class="api_metadata"> 22811cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 22821cb0ef41Sopenharmony_ci</div> 22831cb0ef41Sopenharmony_ci<ul> 22841cb0ef41Sopenharmony_ci<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 22851cb0ef41Sopenharmony_ciit is being dispatched.</li> 22861cb0ef41Sopenharmony_ci</ul> 22871cb0ef41Sopenharmony_ci<p>This is not used in Node.js and is provided purely for completeness.</p> 22881cb0ef41Sopenharmony_ci<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> 22891cb0ef41Sopenharmony_ci<div class="api_metadata"> 22901cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 22911cb0ef41Sopenharmony_ci</div> 22921cb0ef41Sopenharmony_ci<ul> 22931cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 22941cb0ef41Sopenharmony_ci</ul> 22951cb0ef41Sopenharmony_ci<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 22961cb0ef41Sopenharmony_civalue is <code>false</code> in all other cases.</p> 22971cb0ef41Sopenharmony_ci<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> 22981cb0ef41Sopenharmony_ci<div class="api_metadata"> 22991cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 23001cb0ef41Sopenharmony_ci</div> 23011cb0ef41Sopenharmony_ci<p>Sets the <code>defaultPrevented</code> property to <code>true</code> if <code>cancelable</code> is <code>true</code>.</p> 23021cb0ef41Sopenharmony_ci<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> 23031cb0ef41Sopenharmony_ci<div class="api_metadata"> 23041cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 23051cb0ef41Sopenharmony_ci</div> 23061cb0ef41Sopenharmony_ci<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> 23071cb0ef41Sopenharmony_ci<ul> 23081cb0ef41Sopenharmony_ci<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> 23091cb0ef41Sopenharmony_ci</ul> 23101cb0ef41Sopenharmony_ci<p>The value of <code>event.returnValue</code> is always the opposite of <code>event.defaultPrevented</code>. 23111cb0ef41Sopenharmony_ciThis is not used in Node.js and is provided purely for completeness.</p> 23121cb0ef41Sopenharmony_ci<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> 23131cb0ef41Sopenharmony_ci<div class="api_metadata"> 23141cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 23151cb0ef41Sopenharmony_ci</div> 23161cb0ef41Sopenharmony_ci<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> 23171cb0ef41Sopenharmony_ci<ul> 23181cb0ef41Sopenharmony_ci<li>Type: <a href="events.html#class-eventtarget" class="type"><EventTarget></a> The <code>EventTarget</code> dispatching the event.</li> 23191cb0ef41Sopenharmony_ci</ul> 23201cb0ef41Sopenharmony_ci<p>Alias for <code>event.target</code>.</p> 23211cb0ef41Sopenharmony_ci<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> 23221cb0ef41Sopenharmony_ci<div class="api_metadata"> 23231cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 23241cb0ef41Sopenharmony_ci</div> 23251cb0ef41Sopenharmony_ci<p>Stops the invocation of event listeners after the current one completes.</p> 23261cb0ef41Sopenharmony_ci<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> 23271cb0ef41Sopenharmony_ci<div class="api_metadata"> 23281cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 23291cb0ef41Sopenharmony_ci</div> 23301cb0ef41Sopenharmony_ci<p>This is not used in Node.js and is provided purely for completeness.</p> 23311cb0ef41Sopenharmony_ci<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> 23321cb0ef41Sopenharmony_ci<div class="api_metadata"> 23331cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 23341cb0ef41Sopenharmony_ci</div> 23351cb0ef41Sopenharmony_ci<ul> 23361cb0ef41Sopenharmony_ci<li>Type: <a href="events.html#class-eventtarget" class="type"><EventTarget></a> The <code>EventTarget</code> dispatching the event.</li> 23371cb0ef41Sopenharmony_ci</ul> 23381cb0ef41Sopenharmony_ci<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> 23391cb0ef41Sopenharmony_ci<div class="api_metadata"> 23401cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 23411cb0ef41Sopenharmony_ci</div> 23421cb0ef41Sopenharmony_ci<ul> 23431cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 23441cb0ef41Sopenharmony_ci</ul> 23451cb0ef41Sopenharmony_ci<p>The millisecond timestamp when the <code>Event</code> object was created.</p> 23461cb0ef41Sopenharmony_ci<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> 23471cb0ef41Sopenharmony_ci<div class="api_metadata"> 23481cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 23491cb0ef41Sopenharmony_ci</div> 23501cb0ef41Sopenharmony_ci<ul> 23511cb0ef41Sopenharmony_ci<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 23521cb0ef41Sopenharmony_ci</ul> 23531cb0ef41Sopenharmony_ci<p>The event type identifier.</p> 23541cb0ef41Sopenharmony_ci<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> 23551cb0ef41Sopenharmony_ci<div class="api_metadata"> 23561cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 23571cb0ef41Sopenharmony_ci<table> 23581cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 23591cb0ef41Sopenharmony_ci<tr><td>v15.0.0</td> 23601cb0ef41Sopenharmony_ci<td><p>The <code>EventTarget</code> class is now available through the global object.</p></td></tr> 23611cb0ef41Sopenharmony_ci<tr><td>v14.5.0</td> 23621cb0ef41Sopenharmony_ci<td><p><span>Added in: v14.5.0</span></p></td></tr> 23631cb0ef41Sopenharmony_ci</tbody></table> 23641cb0ef41Sopenharmony_ci</details> 23651cb0ef41Sopenharmony_ci</div> 23661cb0ef41Sopenharmony_ci<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> 23671cb0ef41Sopenharmony_ci<div class="api_metadata"> 23681cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 23691cb0ef41Sopenharmony_ci<table> 23701cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 23711cb0ef41Sopenharmony_ci<tr><td>v15.4.0</td> 23721cb0ef41Sopenharmony_ci<td><p>add support for <code>signal</code> option.</p></td></tr> 23731cb0ef41Sopenharmony_ci<tr><td>v14.5.0</td> 23741cb0ef41Sopenharmony_ci<td><p><span>Added in: v14.5.0</span></p></td></tr> 23751cb0ef41Sopenharmony_ci</tbody></table> 23761cb0ef41Sopenharmony_ci</details> 23771cb0ef41Sopenharmony_ci</div> 23781cb0ef41Sopenharmony_ci<ul> 23791cb0ef41Sopenharmony_ci<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 23801cb0ef41Sopenharmony_ci<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> 23811cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 23821cb0ef41Sopenharmony_ci<ul> 23831cb0ef41Sopenharmony_ci<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 23841cb0ef41Sopenharmony_ciwhen it is first invoked. <strong>Default:</strong> <code>false</code>.</li> 23851cb0ef41Sopenharmony_ci<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 23861cb0ef41Sopenharmony_cinot call the <code>Event</code> object's <code>preventDefault()</code> method. 23871cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li> 23881cb0ef41Sopenharmony_ci<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 23891cb0ef41Sopenharmony_cicompleteness. <strong>Default:</strong> <code>false</code>.</li> 23901cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> The listener will be removed when the given 23911cb0ef41Sopenharmony_ciAbortSignal object's <code>abort()</code> method is called.</li> 23921cb0ef41Sopenharmony_ci</ul> 23931cb0ef41Sopenharmony_ci</li> 23941cb0ef41Sopenharmony_ci</ul> 23951cb0ef41Sopenharmony_ci<p>Adds a new handler for the <code>type</code> event. Any given <code>listener</code> is added 23961cb0ef41Sopenharmony_cionly once per <code>type</code> and per <code>capture</code> option value.</p> 23971cb0ef41Sopenharmony_ci<p>If the <code>once</code> option is <code>true</code>, the <code>listener</code> is removed after the 23981cb0ef41Sopenharmony_cinext time a <code>type</code> event is dispatched.</p> 23991cb0ef41Sopenharmony_ci<p>The <code>capture</code> option is not used by Node.js in any functional way other than 24001cb0ef41Sopenharmony_citracking registered event listeners per the <code>EventTarget</code> specification. 24011cb0ef41Sopenharmony_ciSpecifically, the <code>capture</code> option is used as part of the key when registering 24021cb0ef41Sopenharmony_cia <code>listener</code>. Any individual <code>listener</code> may be added once with 24031cb0ef41Sopenharmony_ci<code>capture = false</code>, and once with <code>capture = true</code>.</p> 24041cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">handler</span>(<span class="hljs-params">event</span>) {} 24051cb0ef41Sopenharmony_ci 24061cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> target = <span class="hljs-keyword">new</span> <span class="hljs-title class_">EventTarget</span>(); 24071cb0ef41Sopenharmony_citarget.<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> 24081cb0ef41Sopenharmony_citarget.<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> 24091cb0ef41Sopenharmony_ci 24101cb0ef41Sopenharmony_ci<span class="hljs-comment">// Removes the second instance of handler</span> 24111cb0ef41Sopenharmony_citarget.<span class="hljs-title function_">removeEventListener</span>(<span class="hljs-string">'foo'</span>, handler); 24121cb0ef41Sopenharmony_ci 24131cb0ef41Sopenharmony_ci<span class="hljs-comment">// Removes the first instance of handler</span> 24141cb0ef41Sopenharmony_citarget.<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> 24151cb0ef41Sopenharmony_ci<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> 24161cb0ef41Sopenharmony_ci<div class="api_metadata"> 24171cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 24181cb0ef41Sopenharmony_ci</div> 24191cb0ef41Sopenharmony_ci<ul> 24201cb0ef41Sopenharmony_ci<li><code>event</code> <a href="events.html#class-event" class="type"><Event></a></li> 24211cb0ef41Sopenharmony_ci<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 24221cb0ef41Sopenharmony_cifalse or its <code>preventDefault()</code> method was not invoked, otherwise <code>false</code>.</li> 24231cb0ef41Sopenharmony_ci</ul> 24241cb0ef41Sopenharmony_ci<p>Dispatches the <code>event</code> to the list of handlers for <code>event.type</code>.</p> 24251cb0ef41Sopenharmony_ci<p>The registered event listeners is synchronously invoked in the order they 24261cb0ef41Sopenharmony_ciwere registered.</p> 24271cb0ef41Sopenharmony_ci<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> 24281cb0ef41Sopenharmony_ci<div class="api_metadata"> 24291cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 24301cb0ef41Sopenharmony_ci</div> 24311cb0ef41Sopenharmony_ci<ul> 24321cb0ef41Sopenharmony_ci<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 24331cb0ef41Sopenharmony_ci<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> 24341cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 24351cb0ef41Sopenharmony_ci<ul> 24361cb0ef41Sopenharmony_ci<li><code>capture</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 24371cb0ef41Sopenharmony_ci</ul> 24381cb0ef41Sopenharmony_ci</li> 24391cb0ef41Sopenharmony_ci</ul> 24401cb0ef41Sopenharmony_ci<p>Removes the <code>listener</code> from the list of handlers for event <code>type</code>.</p> 24411cb0ef41Sopenharmony_ci<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> 24421cb0ef41Sopenharmony_ci<div class="api_metadata"> 24431cb0ef41Sopenharmony_ci<span>Added in: v18.7.0</span> 24441cb0ef41Sopenharmony_ci</div> 24451cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental.</div><p></p> 24461cb0ef41Sopenharmony_ci<ul> 24471cb0ef41Sopenharmony_ci<li>Extends: <a href="events.html#class-event" class="type"><Event></a></li> 24481cb0ef41Sopenharmony_ci</ul> 24491cb0ef41Sopenharmony_ci<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>. 24501cb0ef41Sopenharmony_ciInstances are created internally by Node.js.</p> 24511cb0ef41Sopenharmony_ci<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> 24521cb0ef41Sopenharmony_ci<div class="api_metadata"> 24531cb0ef41Sopenharmony_ci<span>Added in: v18.7.0</span> 24541cb0ef41Sopenharmony_ci</div> 24551cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental.</div><p></p> 24561cb0ef41Sopenharmony_ci<ul> 24571cb0ef41Sopenharmony_ci<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> 24581cb0ef41Sopenharmony_ci</ul> 24591cb0ef41Sopenharmony_ci<p>Read-only.</p> 24601cb0ef41Sopenharmony_ci<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> 24611cb0ef41Sopenharmony_ci<div class="api_metadata"> 24621cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 24631cb0ef41Sopenharmony_ci</div> 24641cb0ef41Sopenharmony_ci<ul> 24651cb0ef41Sopenharmony_ci<li>Extends: <a href="events.html#class-eventtarget" class="type"><EventTarget></a></li> 24661cb0ef41Sopenharmony_ci</ul> 24671cb0ef41Sopenharmony_ci<p>The <code>NodeEventTarget</code> is a Node.js-specific extension to <code>EventTarget</code> 24681cb0ef41Sopenharmony_cithat emulates a subset of the <code>EventEmitter</code> API.</p> 24691cb0ef41Sopenharmony_ci<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> 24701cb0ef41Sopenharmony_ci<div class="api_metadata"> 24711cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 24721cb0ef41Sopenharmony_ci</div> 24731cb0ef41Sopenharmony_ci<ul> 24741cb0ef41Sopenharmony_ci<li> 24751cb0ef41Sopenharmony_ci<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></p> 24761cb0ef41Sopenharmony_ci</li> 24771cb0ef41Sopenharmony_ci<li> 24781cb0ef41Sopenharmony_ci<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> 24791cb0ef41Sopenharmony_ci</li> 24801cb0ef41Sopenharmony_ci<li> 24811cb0ef41Sopenharmony_ci<p>Returns: <a href="events.html#class-eventtarget" class="type"><EventTarget></a> this</p> 24821cb0ef41Sopenharmony_ci</li> 24831cb0ef41Sopenharmony_ci</ul> 24841cb0ef41Sopenharmony_ci<p>Node.js-specific extension to the <code>EventTarget</code> class that emulates the 24851cb0ef41Sopenharmony_ciequivalent <code>EventEmitter</code> API. The only difference between <code>addListener()</code> and 24861cb0ef41Sopenharmony_ci<code>addEventListener()</code> is that <code>addListener()</code> will return a reference to the 24871cb0ef41Sopenharmony_ci<code>EventTarget</code>.</p> 24881cb0ef41Sopenharmony_ci<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> 24891cb0ef41Sopenharmony_ci<div class="api_metadata"> 24901cb0ef41Sopenharmony_ci<span>Added in: v15.2.0</span> 24911cb0ef41Sopenharmony_ci</div> 24921cb0ef41Sopenharmony_ci<ul> 24931cb0ef41Sopenharmony_ci<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 24941cb0ef41Sopenharmony_ci<li><code>arg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li> 24951cb0ef41Sopenharmony_ci<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, 24961cb0ef41Sopenharmony_ciotherwise <code>false</code>.</li> 24971cb0ef41Sopenharmony_ci</ul> 24981cb0ef41Sopenharmony_ci<p>Node.js-specific extension to the <code>EventTarget</code> class that dispatches the 24991cb0ef41Sopenharmony_ci<code>arg</code> to the list of handlers for <code>type</code>.</p> 25001cb0ef41Sopenharmony_ci<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> 25011cb0ef41Sopenharmony_ci<div class="api_metadata"> 25021cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 25031cb0ef41Sopenharmony_ci</div> 25041cb0ef41Sopenharmony_ci<ul> 25051cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> 25061cb0ef41Sopenharmony_ci</ul> 25071cb0ef41Sopenharmony_ci<p>Node.js-specific extension to the <code>EventTarget</code> class that returns an array 25081cb0ef41Sopenharmony_ciof event <code>type</code> names for which event listeners are registered.</p> 25091cb0ef41Sopenharmony_ci<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> 25101cb0ef41Sopenharmony_ci<div class="api_metadata"> 25111cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 25121cb0ef41Sopenharmony_ci</div> 25131cb0ef41Sopenharmony_ci<ul> 25141cb0ef41Sopenharmony_ci<li> 25151cb0ef41Sopenharmony_ci<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></p> 25161cb0ef41Sopenharmony_ci</li> 25171cb0ef41Sopenharmony_ci<li> 25181cb0ef41Sopenharmony_ci<p>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></p> 25191cb0ef41Sopenharmony_ci</li> 25201cb0ef41Sopenharmony_ci</ul> 25211cb0ef41Sopenharmony_ci<p>Node.js-specific extension to the <code>EventTarget</code> class that returns the number 25221cb0ef41Sopenharmony_ciof event listeners registered for the <code>type</code>.</p> 25231cb0ef41Sopenharmony_ci<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> 25241cb0ef41Sopenharmony_ci<div class="api_metadata"> 25251cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 25261cb0ef41Sopenharmony_ci</div> 25271cb0ef41Sopenharmony_ci<ul> 25281cb0ef41Sopenharmony_ci<li><code>n</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 25291cb0ef41Sopenharmony_ci</ul> 25301cb0ef41Sopenharmony_ci<p>Node.js-specific extension to the <code>EventTarget</code> class that sets the number 25311cb0ef41Sopenharmony_ciof max event listeners as <code>n</code>.</p> 25321cb0ef41Sopenharmony_ci<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> 25331cb0ef41Sopenharmony_ci<div class="api_metadata"> 25341cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 25351cb0ef41Sopenharmony_ci</div> 25361cb0ef41Sopenharmony_ci<ul> 25371cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 25381cb0ef41Sopenharmony_ci</ul> 25391cb0ef41Sopenharmony_ci<p>Node.js-specific extension to the <code>EventTarget</code> class that returns the number 25401cb0ef41Sopenharmony_ciof max event listeners.</p> 25411cb0ef41Sopenharmony_ci<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> 25421cb0ef41Sopenharmony_ci<div class="api_metadata"> 25431cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 25441cb0ef41Sopenharmony_ci</div> 25451cb0ef41Sopenharmony_ci<ul> 25461cb0ef41Sopenharmony_ci<li> 25471cb0ef41Sopenharmony_ci<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></p> 25481cb0ef41Sopenharmony_ci</li> 25491cb0ef41Sopenharmony_ci<li> 25501cb0ef41Sopenharmony_ci<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> 25511cb0ef41Sopenharmony_ci</li> 25521cb0ef41Sopenharmony_ci<li> 25531cb0ef41Sopenharmony_ci<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> 25541cb0ef41Sopenharmony_ci<ul> 25551cb0ef41Sopenharmony_ci<li><code>capture</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 25561cb0ef41Sopenharmony_ci</ul> 25571cb0ef41Sopenharmony_ci</li> 25581cb0ef41Sopenharmony_ci<li> 25591cb0ef41Sopenharmony_ci<p>Returns: <a href="events.html#class-eventtarget" class="type"><EventTarget></a> this</p> 25601cb0ef41Sopenharmony_ci</li> 25611cb0ef41Sopenharmony_ci</ul> 25621cb0ef41Sopenharmony_ci<p>Node.js-specific alias for <code>eventTarget.removeEventListener()</code>.</p> 25631cb0ef41Sopenharmony_ci<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> 25641cb0ef41Sopenharmony_ci<div class="api_metadata"> 25651cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 25661cb0ef41Sopenharmony_ci</div> 25671cb0ef41Sopenharmony_ci<ul> 25681cb0ef41Sopenharmony_ci<li> 25691cb0ef41Sopenharmony_ci<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></p> 25701cb0ef41Sopenharmony_ci</li> 25711cb0ef41Sopenharmony_ci<li> 25721cb0ef41Sopenharmony_ci<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> 25731cb0ef41Sopenharmony_ci</li> 25741cb0ef41Sopenharmony_ci<li> 25751cb0ef41Sopenharmony_ci<p>Returns: <a href="events.html#class-eventtarget" class="type"><EventTarget></a> this</p> 25761cb0ef41Sopenharmony_ci</li> 25771cb0ef41Sopenharmony_ci</ul> 25781cb0ef41Sopenharmony_ci<p>Node.js-specific alias for <code>eventTarget.addEventListener()</code>.</p> 25791cb0ef41Sopenharmony_ci<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> 25801cb0ef41Sopenharmony_ci<div class="api_metadata"> 25811cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 25821cb0ef41Sopenharmony_ci</div> 25831cb0ef41Sopenharmony_ci<ul> 25841cb0ef41Sopenharmony_ci<li> 25851cb0ef41Sopenharmony_ci<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></p> 25861cb0ef41Sopenharmony_ci</li> 25871cb0ef41Sopenharmony_ci<li> 25881cb0ef41Sopenharmony_ci<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> 25891cb0ef41Sopenharmony_ci</li> 25901cb0ef41Sopenharmony_ci<li> 25911cb0ef41Sopenharmony_ci<p>Returns: <a href="events.html#class-eventtarget" class="type"><EventTarget></a> this</p> 25921cb0ef41Sopenharmony_ci</li> 25931cb0ef41Sopenharmony_ci</ul> 25941cb0ef41Sopenharmony_ci<p>Node.js-specific extension to the <code>EventTarget</code> class that adds a <code>once</code> 25951cb0ef41Sopenharmony_cilistener for the given event <code>type</code>. This is equivalent to calling <code>on</code> 25961cb0ef41Sopenharmony_ciwith the <code>once</code> option set to <code>true</code>.</p> 25971cb0ef41Sopenharmony_ci<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> 25981cb0ef41Sopenharmony_ci<div class="api_metadata"> 25991cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 26001cb0ef41Sopenharmony_ci</div> 26011cb0ef41Sopenharmony_ci<ul> 26021cb0ef41Sopenharmony_ci<li> 26031cb0ef41Sopenharmony_ci<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></p> 26041cb0ef41Sopenharmony_ci</li> 26051cb0ef41Sopenharmony_ci<li> 26061cb0ef41Sopenharmony_ci<p>Returns: <a href="events.html#class-eventtarget" class="type"><EventTarget></a> this</p> 26071cb0ef41Sopenharmony_ci</li> 26081cb0ef41Sopenharmony_ci</ul> 26091cb0ef41Sopenharmony_ci<p>Node.js-specific extension to the <code>EventTarget</code> class. If <code>type</code> is specified, 26101cb0ef41Sopenharmony_ciremoves all registered listeners for <code>type</code>, otherwise removes all registered 26111cb0ef41Sopenharmony_cilisteners.</p> 26121cb0ef41Sopenharmony_ci<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> 26131cb0ef41Sopenharmony_ci<div class="api_metadata"> 26141cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span> 26151cb0ef41Sopenharmony_ci</div> 26161cb0ef41Sopenharmony_ci<ul> 26171cb0ef41Sopenharmony_ci<li> 26181cb0ef41Sopenharmony_ci<p><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></p> 26191cb0ef41Sopenharmony_ci</li> 26201cb0ef41Sopenharmony_ci<li> 26211cb0ef41Sopenharmony_ci<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> 26221cb0ef41Sopenharmony_ci</li> 26231cb0ef41Sopenharmony_ci<li> 26241cb0ef41Sopenharmony_ci<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p> 26251cb0ef41Sopenharmony_ci<ul> 26261cb0ef41Sopenharmony_ci<li><code>capture</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 26271cb0ef41Sopenharmony_ci</ul> 26281cb0ef41Sopenharmony_ci</li> 26291cb0ef41Sopenharmony_ci<li> 26301cb0ef41Sopenharmony_ci<p>Returns: <a href="events.html#class-eventtarget" class="type"><EventTarget></a> this</p> 26311cb0ef41Sopenharmony_ci</li> 26321cb0ef41Sopenharmony_ci</ul> 26331cb0ef41Sopenharmony_ci<p>Node.js-specific extension to the <code>EventTarget</code> class that removes the 26341cb0ef41Sopenharmony_ci<code>listener</code> for the given <code>type</code>. The only difference between <code>removeListener()</code> 26351cb0ef41Sopenharmony_ciand <code>removeEventListener()</code> is that <code>removeListener()</code> will return a reference 26361cb0ef41Sopenharmony_cito the <code>EventTarget</code>.</p></section> 26371cb0ef41Sopenharmony_ci <!-- API END --> 26381cb0ef41Sopenharmony_ci </div> 26391cb0ef41Sopenharmony_ci </div> 26401cb0ef41Sopenharmony_ci </div> 26411cb0ef41Sopenharmony_ci</body> 26421cb0ef41Sopenharmony_ci</html> 2643