Created
October 26, 2023 16:43
-
-
Save pravynandas/f29470d5baf258f38138bce71066dc51 to your computer and use it in GitHub Desktop.
Event Emitter Example in NodeJs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const EventEmitter = require('events'); | |
class MyStream extends EventEmitter { | |
constructor() { | |
super(); | |
} | |
write(data) { | |
this.emit('data', data); | |
} | |
} | |
const stream = new MyStream(); | |
stream.on('data', (data) => { | |
console.log(`Received data: "${data}"`); | |
}); | |
stream.write('With ES6'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credit: User cmac for this answer