From db52004af90b7b410fef836f7332f06d85c9a9b3 Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Tue, 12 Dec 2017 13:36:05 -0500 Subject: [PATCH] fixup: example for rawListeners --- doc/api/events.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/doc/api/events.md b/doc/api/events.md index 53a96ced5adc54..74ca0cbd3c2f97 100644 --- a/doc/api/events.md +++ b/doc/api/events.md @@ -583,6 +583,26 @@ added: REPLACEME Returns a copy of the array of listeners for the event named `eventName`, including any wrappers (such as those created by `.once`). +```js +const emitter = new EventEmitter(); +emitter.once('log', () => console.log('log once')); + +// Returns a new Array with a function `onceWrapper` which has a property +// `listener` which contains the original listener bound above +const listeners = emitter.rawListeners('log'); +const logFnWrapper = listeners[0]; + +// logs "log once" to the console and does not unbind the `once` event +logFnWrapper.listener(); + +// logs "log once" to the console and removes the listener +logFnWrapper(); + +// will return an empty array +const newListeners = emitter.rawListeners('log'); +assert.strictEqual(newListeners.length, 0); +``` + [`--trace-warnings`]: cli.html#cli_trace_warnings [`EventEmitter.defaultMaxListeners`]: #events_eventemitter_defaultmaxlisteners [`domain`]: domain.html