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
nodejs_events.htm
Advertisements