May 2018
Intermediate to advanced
492 pages
12h 3m
English
The EventEmitter object is defined in the events module of Node.js. Directly using the EventEmitter class means performing require('events'). In most cases, you'll be using an existing object that uses EventEmitter internally and you won't require this module. But there are cases where needs dictate implementing an EventEmitter subclass.
Create a file named pulser.js containing the following code:
const EventEmitter = require('events');class Pulser extends EventEmitter { start() { setInterval(() => { console.log(`${new Date().toISOString()} >>>> pulse`); this.emit('pulse'); console.log(`${new Date().toISOString()} <<<< pulse`); }, 1000); }}module.exports = Pulser;
This defines a Pulser class, which inherits from EventEmitter ...
Read now
Unlock full access