NodeJS - emitter.prependListener() Method


This method will add the function(s) which are passed as listener function to the beginning of the listener array for the event named eventName. There will be no possible way to check if the listener has already been added to the listener array.

If multiple calls of the emitter.prependListener() method passing the same combination of eventName and listener, the listener function(s) will be added to the beginning of the listener array and will be called multiple times.

This method belongs to Eventemitter class is an inbuilt class of node:events module.

Syntax

Following is the syntax of the NodeJs emitter.prependListener() method −

emitter.prependListener(eventName, listener)

Parameters

This method accepts two parameter as described below.

  • eventName: (required) 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: (required) 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.prependListener() 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.prependListener() 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.prependListener("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.prependListener() 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

In this program, we called the same combination of eventName and listener with the emitter.prependListener() method. Thus emitter.prependListener() method will add the listener to the beginning of the listener array and will be called multiple times. When we compile and run the program f2 function will be called multiple times.

const EventEmitter = require('node:events');
const myEmitter = new EventEmitter();

function f1(){
  console.log("Func1 is added");
}

function f2(){
  console.log("Fucn2 is added");
}

myEmitter.on("myEvent", f1);
myEmitter.prependListener("myEvent", f2);
myEmitter.prependListener("myEvent", f2);
myEmitter.prependListener("myEvent", f2);

myEmitter.emit("myEvent");

Output

The above program produces the following output −

Fucn2 is added
Fucn2 is added
Fucn2 is added
Func1 is added
nodejs_events.htm
Advertisements