-
Notifications
You must be signed in to change notification settings - Fork 2
/
event-emmitter.js
20 lines (15 loc) · 753 Bytes
/
event-emmitter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const events = require('events')
// Create an eventEmitter instance
const eventEmitter = new events.EventEmitter()
// Create an event handler as follows
const connectToTheHandler = function connected() {
console.log('Test connection was successful.')
// Fire the data_received_success event
eventEmitter.emit('data_received_success', { connect: 'success' })
}
// Bind the connection_success event with the handler
eventEmitter.on('connection_success', connectToTheHandler)
// Bind the data_received_success event with the anonymous function
eventEmitter.on('data_received_success', (msg) => console.log('Confirmed message has been received successfully.', msg))
// Fire the connection_success event
eventEmitter.emit('connection_success')