I have just started exploring node.js and below is situation with me while learning events handling in node.js.
I have an event 'loop' and a function 'myLoopHandler' attached to it using the method
eventEmitter.on('loop',myLoopHandler);
And the myLoopHandler is defined as follows:
var myLoopHandler = function(){
for(i=1;i<=30;i++)
console.log(i);
}
And then I emit the event 'loop' :
eventEmitter.emit('loop');
How do I pass some parameter to the myLoopHandler function in eventEmitter.on method?
I am open to any other way of achieving the same.
I have just started exploring node.js and below is situation with me while learning events handling in node.js.
I have an event 'loop' and a function 'myLoopHandler' attached to it using the method
eventEmitter.on('loop',myLoopHandler);
And the myLoopHandler is defined as follows:
var myLoopHandler = function(){
for(i=1;i<=30;i++)
console.log(i);
}
And then I emit the event 'loop' :
eventEmitter.emit('loop');
How do I pass some parameter to the myLoopHandler function in eventEmitter.on method?
I am open to any other way of achieving the same.
Share Improve this question asked Aug 14, 2017 at 10:06 Adarsh TrivediAdarsh Trivedi 5822 gold badges9 silver badges24 bronze badges1 Answer
Reset to default 25just do
emitter.emit(eventName[, ...args])
where args is the arguments to emit
this is an example
const myEmitter = new MyEmitter();
myEmitter.on('event', function(a, b) {
console.log(a, b, this);
// Prints:
// a b MyEmitter {
// domain: null,
// _events: { event: [Function] },
// _eventsCount: 1,
// _maxListeners: undefined }
});
myEmitter.emit('event', 'a', 'b');
source NodeJS docs