Skip to content

Commit 84fdb1c

Browse files
jasnelltargos
authored andcommitted
doc: add note about removeListener order
Fixes: #21635 PR-URL: #23762 Reviewed-By: Matheus Marchini <[email protected]> Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent f4c4b2b commit 84fdb1c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

doc/api/events.md

+20
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,26 @@ being removed. This will not impact the order in which listeners are called,
581581
but it means that any copies of the listener array as returned by
582582
the `emitter.listeners()` method will need to be recreated.
583583

584+
When a single function has been added as a handler multiple times for a single
585+
event (as in the example below), `removeListener()` will remove the most
586+
recently added instance. In the example the `once('ping')`
587+
listener is removed:
588+
589+
```js
590+
const ee = new EventEmitter();
591+
592+
function pong() {
593+
console.log('pong');
594+
}
595+
596+
ee.on('ping', pong);
597+
ee.once('ping', pong);
598+
ee.removeListener('ping', pong);
599+
600+
ee.emit('ping');
601+
ee.emit('ping');
602+
```
603+
584604
Returns a reference to the `EventEmitter`, so that calls can be chained.
585605

586606
### emitter.setMaxListeners(n)

0 commit comments

Comments
 (0)