NodeJS - emitter.off() Method


The emitter.off() method will remove the specified listener function from the listener array of the event named eventName.

This method will remove the most recently added instance of a listener function from the listener array. If in case one listener is added multiple times to the listener array of the event named eventName, then the emitter.off() method should be called multiple times to remove each instance from the listener array.

This method belongs to Eventemitter class is an inbuilt class of node:events module.

Syntax

Following is the syntax of the NodeJs emitter.off() method −

emitter.off(eventName, listener)

Parameters

This method accepts two parameters as described below.

  • eventName: This is the first parameter of the method and it holds the name of the event. It can be either string or a symbol.
  • listener: This parameter holds the callback function.

Return value

This method returns a reference to the EventEmitter so that calls can be chained.

Example 1

The following is the basic example of the NodeJs emitter.off() Method.

Here, we created a function f1() with a message inside it by calling the emitter.on() method with an eventName (event) passed as the first parameter and we passed the (f1) to the listener function parameter of the method. Thus the function is added to the listener array. Then we called myEmitter.off() method with the eventName (event) and f1() function. So when we compile and run the program myEmitter.off() method will remove the listener function f1() of event named event.

const EventEmitter = require('node:events');
const myEvent = new EventEmitter();

function one(){
  console('Welcome to Earth');
};

myEvent.on('event', one);

console.log(myEvent.eventNames());

Output

The above program produces the following output −

Program did not output anything!

Example 2

Following is the program passing a similar combination of eventName and listener function to the myEmitter.on() method. This will result in the listener function being added to the listener array and called multiple times. So that the function f1() is added four times to the listener array. Then we called myEmitter.off() method two times with the same combination of the eventName and listener function which we passed in myEmitter.on() method. So only two instances of listener function f1() will be removed.

const EventEmitter = require('node:events');

const myEmitter = new EventEmitter();

function f1(){
    console.log('Welcome to tutorialspoint');
}

myEmitter.on('event', f1);
myEmitter.on('event', f1);
myEmitter.on('event', f1);
myEmitter.on('event', f1);

myEmitter.off('event', f1);
myEmitter.off('event', f1);

myEmitter.emit('event'); 

Output

The above program produces the following output −

Welcome to tutorialspoint
Welcome to tutorialspoint

Example 3

In this program, we added a single listener function (f1) multiple times for a single event (event). Then we called the myEmitter.off() method with an event named event and listener f1(). If we compile and run the program, the off() method will remove the most recently added instance. Here, myEmitter.on(event) will be removed.

const EventEmitter = require('node:events');

const myEmitter = new EventEmitter();

function f1(){
    console.log('Bye');
}

myEmitter.once('event', f1);
myEmitter.addListener('event', f1);
myEmitter.on('event', f1);

myEmitter.off('event', f1);

myEmitter.emit('event');

Output

The above program produces the following output −

Bye
Bye
nodejs_events.htm
Advertisements