Is there a way to tell from within the event handler callback which function and / or object selected (called) the event?
Here is an example program:
var EventEmitter, ee, rand, obj;
EventEmitter = require("events").EventEmitter;
ee = new EventEmitter();
ee.on('do it', cFunc);
obj = {
maybeMe: true,
emitting: function() {
ee.emit('do it');
}
}
function aFunc() {
ee.emit('do it');
}
function bFunc() {
ee.emit('do it');
}
function cFunc() {
console.log('Who called me to do it? aFunc or bFunc or obj (obj.emitting)?');
}
rand = Math.random();
if (rand < .3) {
aFunc();
} else if (rand < .6) {
bFunc();
} else {
obj.emitting();
}
It is also another use if the source of the emitted event is from the node.js. built-in module. For example, the "error" events. If I use a common callback to handle errors, can this callback find out who selected the event that caused it?
source
share