From 2f8098d24e342db6cd7a365b1a53f3862fec9a8f Mon Sep 17 00:00:00 2001 From: James M Snell Date: Fri, 19 Oct 2018 11:57:19 -0700 Subject: [PATCH] doc: add note about removeListener order Fixes: https://github.com/nodejs/node/issues/21635 --- doc/api/events.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/doc/api/events.md b/doc/api/events.md index edb4daa2fbc3b4..aafdbcf735bee5 100644 --- a/doc/api/events.md +++ b/doc/api/events.md @@ -581,6 +581,26 @@ being removed. This will not impact the order in which listeners are called, but it means that any copies of the listener array as returned by the `emitter.listeners()` method will need to be recreated. +When a single function has been added as a handler multiple times for a single +event (as in the example below), `removeListener()` will remove the most +recently added instance. In the example the `once('ping')` +listener is removed: + +```js +const ee = new EventEmitter(); + +function pong() { + console.log('pong'); +} + +ee.on('ping', pong); +ee.once('ping', pong); +ee.removeListener('ping', pong); + +ee.emit('ping'); +ee.emit('ping'); +``` + Returns a reference to the `EventEmitter`, so that calls can be chained. ### emitter.setMaxListeners(n)