Skip to content

Latest commit

 

History

History
 
 

event

Event

let's talk more about events in node

Node.js has ‘events’ module which emits named events that can cause corresponding functions or callbacks to be called

Functions(Callbacks) listen or subscribe to a particular event to occur and when that event triggers, all the callbacks subscribed to that event are fired one by one in order to which they were registered.

The EventEmmitter class

The event can be emitted or listen to an event with the help of EventEmitter.

const EventEmitter=require('events');
var eventEmitter=new EventEmitter();
let's practice more and more !!!!!!

the event has two method :

  • on
  • Emit
but when we use them ?

on for -> subscription

emit for-> publishing

const event = require('events');
class Dog extends event.EventEmitter { }
const dog = new Dog();

dog.on('bark', () => {
  console.log('Woof! Woof!');
});

dog.emit('bark');

output : Woof! Woof!

and there are more methods to use

List of most used methods

  • eventNames: Returns an array of strings of the assigned events.
  • once: Event listener that will be triggered only once.
  • removeListener: Remove a specific listener.
  • removeAllListeners: Remove all listeners from the event.

let me see how you use them :D