
- 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.rawListener() Method
The emitter.rawListeners() method is used to retrieve a copy of the array of listeners for a particular event named eventName. This method also returns an array copy of listeners even if they include any wrappers (which are created by the emitter.once() method).
This method belongs to Eventemitter class is an inbuilt class of node:events module.
Syntax
Following is the syntax of the NodeJs emitter.rawListener() method −
emitter.rawListeners(eventName);
Parameters
This method accepts only one parameter.
- eventName: This parameter holds the event named eventName.
Return value
This method returns a copy of the listeners array for a specific event named eventName. It also returns a copy of the array of listeners for any wrappers which are created by the emitter.once() method.
Example 1
Following is the basic example of the NodeJs emitter.rawListener() Method.
we passed a listener function func() with an eventName myEvent to the emitter.once() method (this method includes wrappers). Then we returned a new array with a function onceWrapper which is having a property listener containing the original listener. Then we logged the text inside the function to the console by calling the eventFnWrapper.listener() and it does not unbind the once event. Once again then we logged the text inside the function to the console by calling eventFnWrapper(), now this will remove the listener.
const EventEmitter = require('node:events'); const emitter = new EventEmitter(); function func(){ console.log('log once'); }; emitter.once('myEvent', func); console.log(emitter.rawListeners('myEvent')); const listeners = emitter.rawListeners('myEvent'); const eventFnWrapper = listeners[0]; eventFnWrapper.listener(); eventFnWrapper(); console.log(emitter.rawListeners('myEvent'));
Output
The above program produces the following output −
[ [Function: bound onceWrapper] { listener: [Function: func] } ] log once log once []
Example 2
In this Program we added the event by emitter.addListener. Then we returned a new array with a single function bound .addListener(). Then we logged the message twice which is inside the listener function func().
const EventEmitter = require('node:events'); const emitter = new EventEmitter(); function func(){ console.log('log once'); }; emitter.addListener('MyEvent', func) const newListeners = emitter.rawListeners('MyEvent'); newListeners[0](); emitter.emit('MyEvent'); console.log(emitter.rawListeners('MyEvent'));
Output
The above program produces the following output −
log once log once [ [Function: func] ]
Example 3
Following is another example where we added both an on listener and an once listener for the 'start' event and retrieved the raw listeners using rawListeners('start'). So, We iterate through the listeners and determine whether each one is an on or once listener.
const EventEmitter = require('node:events'); const EventEmitter = require('events'); const eventEmitter = new EventEmitter(); eventEmitter.on('start', () => { console.log('started'); }); eventEmitter.once('start', () => { console.log('started (once)'); }); const listeners = eventEmitter.rawListeners('start'); listeners.forEach(listener => { if (listener.listener) { console.log('Found an "once" listener:', listener.listener); } else { console.log('Found an "on" listener:', listener); } });
Output
The above program produces the following output −
Found an "on" listener: [Function (anonymous)] Found an "once" listener: [Function (anonymous)]