Reading 16: Event Driven Applications - 401-advanced-javascript-hanna-alemu/seattle-javascript-401d31 GitHub Wiki
What is Event-Driven programming?
Event-Driven Programming is a logical pattern that we can choose to confine our programming within to avoid issues of complexity and collision. Event-Driven Programming makes use of the following concepts:
- An Event Handler is a callback function that will be called when an event is triggered.
- A Main Loop listens for event triggers and calls the associated event handler for that event.
EventEmitter
It allows us to get started incorporating Event-Driven Programming in our project right away.
By default, all listeners attached to a particular event object are called by the EventListener object synchronously in the order in which they are registered or attached to the event object.
- If we want to break that flow and switch to asynchronous mode then we can use setImmediate() or process.nextTick() methods.
Handling event calls in Node.js
The listener can be invoked only once using eventEmitter.once() method while every time the event is emitted using eventEmitter.on() method: