Categories
JavaScript Answers

How to listen to All Emitted Events in Node.js?

Spread the love

To listen to All Emitted Events in Node.js, we create our own Emitter class.

For instance, we write

class Emitter extends require("events") {
  emit(type, ...args) {
    console.log(type + " emitted");
    super.emit(type, ...args);
  }
}

to create the Emitter class that inherits from the events constructor.

And we add our own emit method to get the event type and call super.emit to emit the event.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *