0

I understand events such as onclick or onmosemove, but I find the concept of custom eventemitters a bit puzzling. I understand how it works. What I don't understand is why would someone use it instead of a simple function.

For example: consider the following code.

var events = require('events');
var myEmmitter = new events.EventEmitter();

myEmmitter.on('anEvent',function(msg){
    console.log(msg);
});

myEmmitter.emit('anEvent','The event is absolutely emmited');

Would it not be simpler to create a function and call it where we are emitting the event instead? such as in the following.

myEmitter = function(msg){
    cosole.log(msg);
}

myEmitter('the function is being called where it would have been emitted');
Simple
  • 437
  • 7
  • 19
  • Thank you for the link! – Simple Feb 02 '19 at 21:48
  • 1
    it's about organization and features. For example, being able to sub/unsub many handlers at once, or getting a once() functionality instead of on(), internal logging from one spot, etc. – dandavis Feb 02 '19 at 22:04

0 Answers0