-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path13-Event Emitter.js
More file actions
24 lines (15 loc) · 839 Bytes
/
Copy path13-Event Emitter.js
File metadata and controls
24 lines (15 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//EVENTS:
//As our program executes, it is handled by events. So the flow of the program is event-driven.
//Node.Js uses a Event-Driven Programming. When working in browsers, a big part of our work is handling events.
//The idea is that we are listening for different events and register functions that will execute in response of our specific event
const EventEmitter = require('events');
const customEmitter = new EventEmitter();
// .on method is to listen for a especific event
customEmitter.on('response', (name, id)=>{
console.log(`Data recieved user: ${name} with id: ${id}`);
});
customEmitter.on('response', ()=>{
console.log(`I'm doing some other logic here `);
});
//.emit is to emit. We should place it at the end, because we should first listen and then emit the event
customEmitter.emit("response", "john", 34);