
- 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.prependOnceListener() Method
When we add a listener function by using emitter.once() or emitter.on() methods, it will be added to last in the queue of the listener array and will be called last. By calling the emitter.prependOnceListener() method its adds, and calls, before other listeners.
This method belongs to Eventemitter class is an inbuilt class of node:events module.
Syntax
Following is the syntax of the NodeJs emitter.prependOnceListener() method −
emitter.prependOnceListener(eventName, listener)
Parameters
This method accepts two parameter 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
Following is the basic example of the NodeJs emitter.prependOnceListener() Method.
Here, we created four functions f1, f2, f3, and f4 with a message inside it. Then we called f1, f2, and f3 with myEmitter.on() method. It will add the listener functions as per the order they were called. Later we called listener function f4 with the emitter.prependOnceListener() method. So f4 will be added to the beginning of the listener array. If we compile and run the program, f4 will be returned first and then the rest.
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); function f1(){ console.log("Func1 is added"); } function f2(){ console.log("Fucn2 is added"); } function f3(){ console.log("Fucn3 is added"); } function f4(){ console.log("Fucn4 is added"); } myEmitter.on("myEvent", f1); myEmitter.on("myEvent", f2); myEmitter.on("myEvent", f3); myEmitter.prependOnceListener("myEvent", f4); myEmitter.emit("myEvent");
Output
The above program produces the following output −
Fucn4 is added Func1 is added Fucn2 is added Fucn3 is added
Example 2
In this program, we created four functions f1, f2, f3, and f4 with a message in it. Then we called all four functions with myEmitter.on() method and we didnt call any function with the emitter.prependOnceListener() method. So, when we compile and run the program the myEmitter.on() method will return the listener functions according to the order called.
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); function f1(){ console.log("Func1 is added"); } function f2(){ console.log("Fucn2 is added"); } function f3(){ console.log("Fucn3 is added"); } function f4(){ console.log("Fucn4 is added"); } myEmitter.on("myEvent", f1); myEmitter.on("myEvent", f2); myEmitter.on("myEvent", f3); myEmitter.on("myEvent", f4); myEmitter.emit("myEvent");
Output
The above program produces the following output −
Func1 is added Fucn2 is added Fucn3 is added Fucn4 is added
Example 3
The emitter.prependOnceListener() will add a one-time listener for the eventName anEvent to the beginning of the listeners array. When we compile and run the program, it will add the listener function to the beginning of the listener array.
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); myEmitter.prependOnceListener("anEvent", (f1) => { console.log('Aye, nice to meet you'); }); myEmitter.prependOnceListener("anEvent", (f1) => { console.log('Aye, Hello'); }); myEmitter.emit("anEvent");
Output
The above program produces the following output −
Aye, Hello Aye, nice to meet you