I'm having some trouble with a custom event emitter in Nodejs. I'm pretty sure I've created the eventemitter correctly, since I can fire and catch events on its instances.
However, none of the events in its constructor are being fired. They all seem to exist and are registering their listeners correctly, but the this.emit function doesn't do anything.
For clarity, here's a minimum failing example:
var EventEmitter = require("events").EventEmitter,
util = require("util");
var MyEmitter = function() {
var self = this;
EventEmitter.call(this);
someExternalFunction(function callback() {
self.emit("event 1"); // Does not fire
console.log("fires");
});
this.emit("event 2"); // Does not fire
return this;
};
util.inherits(Browser,EventEmitter);
var myEmitterInstance = new MyEmitter();
myEmitterInstance.emit("event 3"); // fires
myEmitterInstance.on("event 1 - does not fire", function () {
//Do something with the results of someExternalFunction.
} // this function never runs.
I can't understand what I've done wrong.