Last active
March 3, 2018 21:23
-
-
Save dominicbartl/6318101 to your computer and use it in GitHub Desktop.
2 ways to inherit from EventEmitter in Node.JS
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; | |
module.exports = new EventEmitter(); | |
exports.emitSomethingLater = function() | |
setTimeout(function() { | |
module.exports.emit('something'); | |
}, 1000); | |
} |
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 util = require("util"), | |
events = require("events"); | |
function CustomEmitter (opts){ | |
events.EventEmitter.call(this); | |
} | |
util.inherits(CustomEmitter, events.EventEmitter); | |
CustomEmitter.prototype.emitSomething = function() { | |
this.emit('something'); | |
}; | |
module.exports = CustomEmitter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment