
- 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.removeAllListener() Method
The emitter.removeAllListeners() method will help us to remove all the listener function(s) that belongs to a particular event.
It is better to use the emitter.removeListener() method instead of the emitter.removeAllListeners() because it may cause trouble if listeners are added randomly in the code. So, it is better to remove the only listener which needs to be removed and that can happen by using the emitter.removeListener() method.
This method belongs to Eventemitter class is an inbuilt class of node:events module.
Syntax
Following is the syntax of the NodeJs emitter.removeAllListener() method −
emitter.removeAllListeners([eventName]);
Parameters
This method accepts two parameters listed below −
- eventName: (Required) This parameter will hold the particular eventName whose entire listeners are to be deleted.
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 emitter.removeAllListeners() Method.
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); myEmitter.addListener('myEvent', function f1(){ }); myEmitter.removeAllListeners('myEvent');
Output
The above program produces the following output −
Program did not output anything!
Example 2
Following is the another example where we have created the instance of EventEmitter class named myemitter by assigning four listeners (f1, f2, f3 and f4) are added for the event named 'myEvent', and those assigned listeners are removed using removeAllListeners() method.
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); myEmitter.addListener('myEvent', function f1(){ }); myEmitter.addListener('myEvent', function f2(){ }); myEmitter.addListener('myEvent', function f3(){ }); myEmitter.addListener('myEvent', function f4(){ }); myEmitter.removeAllListeners('myEvent');
Output
The above program produces the following output −
Program did not output anything!
Example 3
In the following program, an EventEmitter instance named myEmitter is created and four listeners (f1, f2, f3, and f4) are added for two events: 'myEvent1' and 'myEvent2'. then all the listeners are moved using removeAllListeners().
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); myEmitter.addListener('myEvent1', function f1(){ }); myEmitter.addListener('myEvent1', function f2(){ }); myEmitter.addListener('myEvent2', function f3(){ }); myEmitter.addListener('myEvent2', function f4(){ }); myEmitter.removeAllListeners('myEvent1'); myEmitter.removeAllListeners('myEvent2');
Output
The above program produces the following output −
Program did not output anything!