
- 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.once() Method
The emitter.once() method is used to add one-time listener function(s) for the event named eventName. There is no possible way to check if the listener has already been added to the listener array.
If multiple calls of the emitter.once() method passing the same combination of eventName and listener, the listener function(s) will be added to the listeners array and will be called the number of times it is added.
This method belongs to Eventemitter class is an inbuilt class of node:events module.
Syntax
Following is the syntax of the NodeJs emitter.once() method −
eventEmitter.once(event, listener)
Parameters
This method accepts two parameters listed below −
- EventName: It is a string or a symbol that is representing the name of the event.
- Listener:It is called when the event is emitted for the first time. The listener function can accept arguments that will be passed when the event is emitted.
Return value
This method returns an event listener for a specific event. When the specified event is emitted, the provided callback function is executed.
Example 1
The following is the basic example of the NodeJs eventemitter.once(event, listener) Method.
Initially, we imported the node:events module. We created a listener function one() with a message inside it. Then we called myEmitter.once() method by passing an eventName (event) to the first parameter and function one() to the second parameter. If we compile and run the program, the myEmitter.once() method will return the listener (function one()) stored in the listener array.const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); function one(){ console.log('First user login successful...'); } myEmitter.once('event', one); myEmitter.emit('event');
Output
The above program produces the following output −
First user login successful...
Example 2
In the example, we created three functions and passed them to the listener function parameter of myEmitter.once() method. So the method myEmitter.on() adds the listener functions to the end of the listeners array for the event named event.So when we compile and run the program it will first print the one(), two(), and then three().
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); function one(){ console.log('First user login successful'); } function two(){ console.log('Second user login successful'); } function three(){ console.log('Third user login successful'); } myEmitter.once('event', one); myEmitter.once('event', two); myEmitter.once('event', three); myEmitter.emit('event');
Output
The above program produces the following output −
First user login successful Second user login successful Third user login successful
Example 3
Note: By default, event listeners are invoked in the order they are added.
In the example below, we used emitter.prependOnceListener() method with the same eventName which we passed to the previous myEmitter.once() method. Then we passed a function four() to the emitter.prependOnceListener() method. So this will add the function to the beginning of the listeners array. So when we compile and run the program, the function passed inside emitter.prependOnceListener() method will be printed first and then the rest.
const EventEmitter = require('node:events'); const myEmitter = new EventEmitter(); function one(){ console.log("Func1 is added"); } function two(){ console.log("Fucn2 is added"); } function three(){ console.log("Fucn3 is added"); } function four(){ console.log("Fucn4 is added"); } myEmitter.once("myEvent", one); myEmitter.once("myEvent", two); myEmitter.once("myEvent", three); myEmitter.prependOnceListener("myEvent", four); myEmitter.emit("myEvent");
Output
The above program produces the following output −
Fucn4 is added Func1 is added Fucn2 is added Fucn3 is added