Skip to content

Instantly share code, notes, and snippets.

@DWboutin
Created November 10, 2014 14:22
Show Gist options
  • Save DWboutin/cd64a60f560f472b71df to your computer and use it in GitHub Desktop.
Save DWboutin/cd64a60f560f472b71df to your computer and use it in GitHub Desktop.
Create custom event with Node.js
var events = require('events');
var emitter = new events.EventEmitter();
function taskStatus(status) {
if (status === 'done') {
emitter.emit('done');
} else if (status === 'doing') {
emitter.emit('doing');
}
}
emitter.on('done', function() {
console.log('The task given is done!');
});
emitter.on('doing', function() {
console.log('Doing the given task!');
});
setTimeout(taskStatus, 200, 'done');
setTimeout(taskStatus, 500, 'doing');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment