How to use the EventEmitter function from events

Find comprehensive JavaScript events.EventEmitter code examples handpicked from public code repositorys.

events.EventEmitter is a Node.js class that allows objects in a program to emit and listen for named events.

252
253
254
255
256
257
258
259
260
	urls,
	useESNextSyntax,
	warnings,

	extraModules = [],
	emitter = new events.EventEmitter();

function checkOption(name, t) {
	name = name.trim();
fork icon12
star icon11
watch icon2

148
149
150
151
152
153
154
155
156
157
158
159
}


////////////////////////////////////////////////////////////////////////////////


function mockSocket() {
  const socket = new events.EventEmitter();
  socket.remoteAddress = '127.0.0.1',
  socket.destroy = () => {};
  socket.setEncoding = () => {};
  socket.setKeepAlive = () => {};
fork icon6
star icon1
watch icon1

+ 5 other calls in file

How does events.EventEmitter work?

The events.EventEmitter class in Node.js provides a way to implement the observer pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes. It allows for the creation and registration of custom events with associated listeners, which are executed when an event is emitted. The class provides a set of methods to interact with the events, such as on(), emit(), removeListener(), and more.

1415
1416
1417
1418
1419
1420
1421
    });
  };
};


module.exports = Pool;
Pool.prototype.__proto__ = events.EventEmitter.prototype;
fork icon6
star icon1
watch icon1

48
49
50
51
52
53
54
55
56
57
  this._connection._objects.set(guid, this);
  if (this._parent) {
    this._parent._objects.set(guid, this);
    this._logger = this._parent._logger;
  }
  this._channel = this._createChannel(new _events.EventEmitter());
  this._initializer = initializer;
}
_setEventToSubscriptionMapping(mapping) {
  this._eventToSubscriptionMapping = mapping;
fork icon0
star icon0
watch icon1

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
const EventEmitter = require("events");

// create an instance of EventEmitter
const myEmitter = new EventEmitter();

// listen for an event called 'greeting'
myEmitter.on("greeting", (name) => {
  console.log(`Hello, ${name}!`);
});

// emit the 'greeting' event with the argument 'Alice'
myEmitter.emit("greeting", "Alice"); // Output: Hello, Alice!

In this example, we first import the events module and create a new instance of EventEmitter called myEmitter. We then listen for an event called greeting using the on method, which takes a string with the name of the event to listen for, and a callback function to execute when the event is emitted. Finally, we emit the greeting event with the argument 'Alice' using the emit method, which triggers the callback function we defined earlier and logs Hello, Alice! to the console.

14
15
16
17
18
19
20
21
22
23
    index ++;
  }
  done();
});
//mock aws exceptions
var stream = new EventEmitter();
spyOn(AWS.HttpClient, 'getInstance');
AWS.HttpClient.getInstance.andReturn({
  handleRequest: function(req, opts, cb, errCb) {
    const resp = resps[index];
fork icon0
star icon0
watch icon1

7
8
9
10
11
12
13
14
15
16
class AsyncReader extends reader_1.default {
    constructor(_root, _settings) {
        super(_root, _settings);
        this._settings = _settings;
        this._scandir = fsScandir.scandir;
        this._emitter = new events_1.EventEmitter();
        this._queue = fastq(this._worker.bind(this), this._settings.concurrency);
        this._isFatalError = false;
        this._isDestroyed = false;
        this._queue.drain = () => {
fork icon0
star icon0
watch icon1

+ 3 other calls in file

9
10
11
12
13
14
15
16
17
18
19
20
  rejects,
} = require('assert');
const { EventTarget, Event } = require('internal/event_target');


async function onceAnEvent() {
  const ee = new EventEmitter();


  process.nextTick(() => {
    ee.emit('myevent', 42);
  });
fork icon0
star icon0
watch icon0

+ 8 other calls in file