Skip to content

Latest commit

 

History

History
26 lines (22 loc) · 811 Bytes

section11.2.md

File metadata and controls

26 lines (22 loc) · 811 Bytes

Section 11.2: Get the names of the events that are subscribed to

The function EventEmitter.eventNames() will return an array containing the names of the events currently subscribed to.

let EventEmitter = require("events");
class MyEmitter extends EventEmitter{};

let emitter = new MyEmitter();
emitter
  .on("message", () => { //listen for message event
    console.log("a message was emitted!");
  })
  .on("message", () => { //listen for message event
    console.log("this is not the right message");
  })
  .on("data", function(){ //listen for data event
    console.log("a data just occurred!!");
  }
);

console.log(emitter.eventNames()); //=> ["message","data"]
emitter.removeAllListeners("data");//=> removeAllListeners to data event
console.log(emitter.eventNames()); //=> ["message"]