-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_driven.js
52 lines (38 loc) · 1.07 KB
/
event_driven.js
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
51
52
import EventEmitter from 'events';
import inquirer from 'inquirer';
const myEmitter = new EventEmitter();
const welcomeUser = () => {
console.log('Hello User!');
}
const loadOS = () => {
console.log('Loading OS....');
}
const shutDown = () => {
console.log('System shutting down...');
}
const greetBirthday = (name, newAge) => {
console.log(`Happy Birthday ${name}. You are now ${newAge}!`);
}
// Subscribing to events
myEmitter.on('systemBoot', loadOS);
myEmitter.on('systemBoot', welcomeUser);
myEmitter.on('systemShutdown', shutDown);
myEmitter.on('greetBirthday', greetBirthday);
inquirer
.prompt(
[{
type: 'list',
message: 'Select an event',
name: 'action',
choices: ["Boot System", "Shutdown System"]
}]
)
.then((answers) => {
//Emiting Events
if (answers.action == "Boot System") {
myEmitter.emit('systemBoot')
myEmitter.emit('greetBirthday', 'Mark Dionnie', '24');
} else {
myEmitter.emit('systemShutdown');
}
});