-
-
Save poorcoder/20468595d19eb918cdd587ab775bc412 to your computer and use it in GitHub Desktop.
Using Event Emitter in your node modules
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
var Fancy = require('FancyModule'); | |
var mod = new Fancy(); | |
mod.on('success', function(data) { | |
console.log(data); // { this_is_fancy:'indubitably' } | |
}); |
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
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