Skip to content

Instantly share code, notes, and snippets.

@poorcoder
Forked from fideloper/app.js
Created September 13, 2016 05:40
Show Gist options
  • Save poorcoder/20468595d19eb918cdd587ab775bc412 to your computer and use it in GitHub Desktop.
Save poorcoder/20468595d19eb918cdd587ab775bc412 to your computer and use it in GitHub Desktop.
Using Event Emitter in your node modules
var Fancy = require('FancyModule');
var mod = new Fancy();
mod.on('success', function(data) {
console.log(data); // { this_is_fancy:'indubitably' }
});
var EventEmitter = require('events').EventEmitter;
function FancyModule() {
EventEmitter.call(this);
// And other fancy code
}
FancyModule.prototype = Object.create(EventEmitter.prototype);
FancyModule.prototype.fancified = function() {
// Just one of many fancy functions available in my fancy module
var fancyData = { this_is_fancy:'indubitably' };
this.emit('success', fancyData);
}
module.exports = FancyModule;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment