In a topic about EventEmitter
, someone said that in the following example, there is no need to use events. it is meaningless:
const events = require("events")
const eventEmitter = new events.EventEmitter()
eventEmitter.on("say_hi", () => {console.log("sa")})
eventEmitter.emit("say_hi")
He had said:
When you build a library or internal API, it allows other parts of your code (or people using your code) to subscribe to events without you needing to know this in advance.
For the case you used it above, it does not make sense.
But I did not understand the meaning of his sentence at all.
I did not know at all when to use the EventEmitter
and when to use the Function
.
In another topic, he implemented his example with both EventEmitter
and Function
. There was no difference in execution
except that the event was assigned to an object (called student_max) and if it was to be implemented for another student, the event had to be rewritten (ie repeated) but in function all objects (all students) could Use the score method.
Can someone clearly explain by example and code when to use EventEmitter
and when to use Function
?
The best and clearest answer is in this link and THIS.
In general, we should look at Clean Code
for events, and Open/Closed
Principle(decoupling) is one of the SOLID
principles.