Instructions for the events.emb.listeners method in node.js

  • 2020-03-30 04:36:01
  • OfStack

Method description:

All listeners that registered the specified event will be returned as an array.

Grammar:


emitter.listeners(event)

Receiving parameters:

Event      The specified event

Example:


server.on('connection', function (stream) {
  console.log('someone connected!');
});
console.log(util.inspect(server.listeners('connection')));
// [ [Function] ]

Source:


EventEmitter.prototype.listeners = function(type) {
  var ret;
  if (!this._events || !this._events[type])
    ret = [];
  else if (util.isFunction(this._events[type]))
    ret = [this._events[type]];
  else
    ret = this._events[type].slice();
  return ret;
};


Related articles: