I am new to Node, and I am struggling to understand the main difference between Events and Functions. Both need to be triggered, so why do we need an Event at all if we have to trigger it anyway?
How is it different than having a Function triggered?
Example code:
var events = require('events');
var eventEmitter = new events.EventEmitter();
eventEmitter.on('event1', function () {
console.log('Event 1 executed.');
eventEmitter.emit('event2');
});
eventEmitter.on('event2', function() {
console.log('Event 2 executed.');
});
eventEmitter.emit('event1');
console.log('Program Ended.');
We can achieve the same result by functions, right?
I am sure this has some serious importance in Node (otherwise it would not exist, lol), but I am struggling to understand it.
Help appreciated! :)