Skip to content

Instantly share code, notes, and snippets.

@pravynandas
Created October 26, 2023 16:43
Show Gist options
  • Save pravynandas/f29470d5baf258f38138bce71066dc51 to your computer and use it in GitHub Desktop.
Save pravynandas/f29470d5baf258f38138bce71066dc51 to your computer and use it in GitHub Desktop.
Event Emitter Example in NodeJs
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');
@pravynandas
Copy link
Author

Credit: User cmac for this answer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment