Skip to content

Commit e5f75cf

Browse files
jasnellrvagg
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 0ff88a3 commit e5f75cf

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
@@ -557,6 +557,26 @@ being removed. This will not impact the order in which listeners are called,
557557
but it means that any copies of the listener array as returned by
558558
the `emitter.listeners()` method will need to be recreated.
559559

560+
When a single function has been added as a handler multiple times for a single
561+
event (as in the example below), `removeListener()` will remove the most
562+
recently added instance. In the example the `once('ping')`
563+
listener is removed:
564+
565+
```js
566+
const ee = new EventEmitter();
567+
568+
function pong() {
569+
console.log('pong');
570+
}
571+
572+
ee.on('ping', pong);
573+
ee.once('ping', pong);
574+
ee.removeListener('ping', pong);
575+
576+
ee.emit('ping');
577+
ee.emit('ping');
578+
```
579+
560580
Returns a reference to the `EventEmitter`, so that calls can be chained.
561581

562582
### emitter.setMaxListeners(n)

0 commit comments

Comments
 (0)