Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2a442b3

Browse filesBrowse files
vaibhav93rvagg
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 <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent f6ee099 commit 2a442b3
Copy full SHA for 2a442b3

File tree

Expand file treeCollapse file tree

2 files changed

+56
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+56
-0
lines changed
Open diff view settings
Collapse file

‎doc/api/events.markdown‎

Copy file name to clipboardExpand all lines: doc/api/events.markdown
+37Lines changed: 37 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
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,
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-event-emitter-remove-listeners.js
+19Lines changed: 19 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.