diff --git a/lib/events.js b/lib/events.js index f0b323e15c1751..171f4868a66dac 100644 --- a/lib/events.js +++ b/lib/events.js @@ -425,9 +425,9 @@ EventEmitter.prototype.listeners = function listeners(type) { if (!evlistener) ret = []; else if (typeof evlistener === 'function') - ret = [evlistener]; + ret = [evlistener.listener || evlistener]; else - ret = arrayClone(evlistener, evlistener.length); + ret = unwrapListeners(evlistener); } return ret; @@ -475,3 +475,11 @@ function arrayClone(arr, i) { copy[i] = arr[i]; return copy; } + +function unwrapListeners(arr) { + const ret = new Array(arr.length); + for (var i = 0; i < ret.length; ++i) { + ret[i] = arr[i].listener || arr[i]; + } + return ret; +} diff --git a/test/parallel/test-event-emitter-listeners.js b/test/parallel/test-event-emitter-listeners.js index dfd66287b194a7..cfb2dafb121e03 100644 --- a/test/parallel/test-event-emitter-listeners.js +++ b/test/parallel/test-event-emitter-listeners.js @@ -36,3 +36,16 @@ function listener2() {} assert.deepStrictEqual(ee.listeners('foo'), [listener, listener2]); assert.deepStrictEqual(eeListenersCopy, [listener]); } + +{ + const ee = new events.EventEmitter(); + ee.once('foo', listener); + assert.deepStrictEqual(ee.listeners('foo'), [listener]); +} + +{ + const ee = new events.EventEmitter(); + ee.on('foo', listener); + ee.once('foo', listener2); + assert.deepStrictEqual(ee.listeners('foo'), [listener, listener2]); +}