
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
NodeJs - emitter.removeListener() Method
The emitter.removeListener() method is used to 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 removeListener() 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.removeListener() method −
emitter.removeListener(eventName, listener)
Parameters
This method accepts two parameters listed below −
- EventName: (required) 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: (required) This parameter holds the callback function.
Return value
This method returns a reference to the EventEmitter so that calls can be chained.
Now lets get into the examples of the emitter.removeListener() method of Node.js in different scenarios.
Example 1
Following is the basic example of the NodeJs emitter.removeListener(event, listener) Method.
We created a function f1() with a message inside it. Then we called the emitter.addListener() 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.removeListener() method with eventName (event) and f1() function. So when we compile and run the program myEmitter.removeListener() method will remove the listener function f1() of event named event.
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); function f1(){ console.log('I\'m from function1'); } myEmitter.addListener('event', f1); myEmitter.removeListener('event', f1); myEmitter.emit('event');
Output
The above program produces the following output −
Program did not output anything!
Example 2
In this program, we are passing the same combination of eventName and listener function to the myEmitter.addListener() method. This will result in the listener function being added to the listener array and called multiple times.
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); function f1(){ console.log('I\'m from function1'); } myEmitter.addListener('event1', f1); myEmitter.addListener('event2', f1); myEmitter.addListener('event3', f1); myEmitter.addListener('event4', f1); myEmitter.removeListener('event1', f1); myEmitter.removeListener('event2', f1); myEmitter.emit('event3'); myEmitter.emit('event4');
Output
The above program produces the following output −
I'm from function1 I'm from function1
Example 3
Note: By default, event listeners are invoked in the order they are added.
We added a single listener function (f1) multiple times for a single event (event). Then we called the myEmitter.removeListener() with an event named event and listener f1().
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); function f1(){ console.log('Hi'); } myEmitter.addListener('event', f1); myEmitter.on('event', f1); myEmitter.once('event', f1); myEmitter.removeListener('event', f1); myEmitter.emit('event');
Output
The above program produces the following output −
Hi Hi