Skip to content

Commit 87b8c3b

Browse files
vaibhav93evanlucas
authored andcommitted
doc: update removeListener behaviour
This commit updates events doc to describe removeListener behaviour when it is called within a listener. An example is added to make it more evident. A test is also incuded to make this behaviour consistent in future releases. Fixes: #4759 PR-URL: #5201 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 680db5b commit 87b8c3b

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

doc/api/events.markdown

+37
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,43 @@ listener array. If any single listener has been added multiple times to the
377377
listener array for the specified `event`, then `removeListener` must be called
378378
multiple times to remove each instance.
379379

380+
Note that once an event has been emitted, all listeners attached to it at the
381+
time of emitting will be called in order. This implies that any `removeListener()`
382+
or `removeAllListeners()` calls *after* emitting and *before* the last listener
383+
finishes execution will not remove them from `emit()` in progress. Subsequent
384+
events will behave as expected.
385+
386+
```js
387+
const myEmitter = new MyEmitter();
388+
389+
var callbackA = () => {
390+
console.log('A');
391+
myEmitter.removeListener('event', callbackB);
392+
};
393+
394+
var callbackB = () => {
395+
console.log('B');
396+
};
397+
398+
myEmitter.on('event', callbackA);
399+
400+
myEmitter.on('event', callbackB);
401+
402+
// callbackA removes listener callbackB but it will still be called.
403+
// Interal listener array at time of emit [callbackA, callbackB]
404+
myEmitter.emit('event');
405+
// Prints:
406+
// A
407+
// B
408+
409+
// callbackB is now removed.
410+
// Interal listener array [callbackA]
411+
myEmitter.emit('event');
412+
// Prints:
413+
// A
414+
415+
```
416+
380417
Because listeners are managed using an internal array, calling this will
381418
change the position indices of any listener registered *after* the listener
382419
being removed. This will not impact the order in which listeners are called,

test/parallel/test-event-emitter-remove-listeners.js

+19
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,22 @@ e5.once('removeListener', common.mustCall(function(name, cb) {
8383
}));
8484
e5.removeListener('hello', listener1);
8585
assert.deepEqual([], e5.listeners('hello'));
86+
87+
const e6 = new events.EventEmitter();
88+
89+
const listener3 = common.mustCall(() => {
90+
e6.removeListener('hello', listener4);
91+
}, 2);
92+
93+
const listener4 = common.mustCall(() => {}, 1);
94+
95+
e6.on('hello', listener3);
96+
e6.on('hello', listener4);
97+
98+
// listener4 will still be called although it is removed by listener 3.
99+
e6.emit('hello');
100+
// This is so because the interal listener array at time of emit
101+
// was [listener3,listener4]
102+
103+
// Interal listener array [listener3]
104+
e6.emit('hello');

0 commit comments

Comments
 (0)