Created
October 10, 2019 10:30
-
-
Save miguelmota/9626eafbcce9a339da3e79ad0e730665 to your computer and use it in GitHub Desktop.
Node.js EventEmitter example
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').EventEmitter | |
const myEmitter = new EventEmitter() | |
myEmitter | |
.on('foo', (result) => { | |
// after 1 second | |
console.log(result) // { message: 'hello world' } | |
}) | |
.on('bar', (result) => { | |
console.log(result) // { message: 'hello mars' } | |
}) | |
myEmitter.emit('bar', {message: 'hello mars'}) | |
setTimeout(() => { | |
myEmitter.emit('foo', {message: 'hello world'}) | |
}, 1e3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment