Node.js Events:
--------------
Events Module :
Node.js has a built-in module, called "Events", where you can create-,
fire-, and
listen for- your own events.
To include the built-in Events module use the require() method.
In addition, all event properties and methods are an instance of an EventEmitter object.
To be able to access these properties and methods, create an EventEmitter object.
The EventEmitter Object:
You can assign event handlers to your own events with the EventEmitter object.
To fire an event, use the emit() method.
Nested calling of custom event .
-----------------------------------------------------------------------------------------------------------------------------------------
Node.js Events
Node.js provides an `events` module that allows you to work with custom events in your applications. The module is built around the `EventEmitter` class, which provides methods to create, fire, and listen for events.
# Including the Events Module
To use the `events` module, you need to include it in your project using the `require()` function:
javascript:
const events = require('events');
const EventEmitter = events.EventEmitter;
# The `EventEmitter` Object
The `EventEmitter` object is at the core of the event-driven architecture in Node.js. You can create an instance of `EventEmitter` and use it to manage custom events.
```javascript
const eventEmitter = new EventEmitter();
```
# Assigning Event Handlers
You can assign a function to an event using the `on()` method:
```javascript
// Assign a handler to the 'greet' event
eventEmitter.on('greet', () => {
console.log('Hello, world!');
});
```
# Firing an Event
To fire an event, use the `emit()` method:
```javascript
// Fire the 'greet' event
eventEmitter.emit('greet');
```
# Example of Nested Calling of Custom Events
You can also emit events within other event handlers, leading to nested event calls. Here's an example:
```javascript
const eventEmitter = new EventEmitter();
// Assign a handler to the 'start' event
eventEmitter.on('start', () => {
console.log('Start event triggered.');
// Emit the 'process' event from within the 'start' event handler
eventEmitter.emit('process');
});
// Assign a handler to the 'process' event
eventEmitter.on('process', () => {
console.log('Process event triggered.');
// Emit the 'end' event from within the 'process' event handler
eventEmitter.emit('end');
});
// Assign a handler to the 'end' event
eventEmitter.on('end', () => {
console.log('End event triggered.');
});
// Fire the 'start' event, which will trigger the nested events
eventEmitter.emit('start');
```
# Output of the Nested Events Example
When you run the above code, the output will be:
```
Start event triggered.
Process event triggered.
End event triggered.
```
# Summary
- The Node.js `events` module allows you to create and manage custom events using the `EventEmitter` object.
- You can listen for events using `on()` and fire events using `emit()`.
- Nested event calls are possible, where one event handler can trigger another event, leading to a sequence of event emissions.
Comments
Post a Comment