-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathexample-advanced.js
More file actions
50 lines (38 loc) · 1.66 KB
/
example-advanced.js
File metadata and controls
50 lines (38 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// import the base? class, EventEmitter from the events module
const { EventEmitter } = require('events')
// create some listener functions to handle when the events occur.
// `foo` is expecting two parameters to be passed in.
const foo = function foo(baz, qux) {
console.log(`foo executed. baz: ${baz}, qux: ${qux}`)
}
const bar = function bar() {
console.log('bar executed.')
}
const errorProne = function foo(obj) {
this.emit('error', 'I am just not going to work')
}
// create an emitter and bind some events to it
const eventEmitter = new EventEmitter()
// Bind the connection event with the listner1 function
eventEmitter.addListener('foo', foo)
// N.B. `addListener` is the same as `on`.
// Bind the connection event with the listner2 function -
// IMPORTANT!! Listeners are called synchronously in the order in which they were added.
// - i.e. `foo` will be called, and `bar` will be called once `foo` has completed.
// This is to prevent race conditions and logic errors.
// They can switch to an async mode using `setImmediate`.
eventEmitter.once('bar', bar)
eventEmitter.once('liveDangerously', errorProne)
// handle errors in one of the listeners handlers
eventEmitter.on('error', (err) => {
console.error('There was an error!')
})
for(let i = 0; i < 3; i++ ) {
eventEmitter.emit('foo', 'value1', 'value2')
eventEmitter.emit('bar')
eventEmitter.emit('liveDangerously')
}
const eventListeners = EventEmitter.listenerCount(eventEmitter, 'foo')
console.log(EventEmitter.listenerCount(eventEmitter, 'connection') + " Listner(s) listening to connection event")
console.log(eventListeners + " Listner(s) listening to connection event")
console.log('end of file...')