Event Driven Applications - mwilkin-401-advanced-javascript/bend-javascript-401d2 GitHub Wiki
An event-driven application is a computer program that is written to respond to actions generated by the user or the system. An event is any identifiable occurrence that has significance for system hardware or software, including both user-generated actions like mouse clicks and keystrokes and system-generated events such as program loading.
Event-driven programming is a style of programming which follows a logical pattern that should help avoid complexity and collisions, and can improve responsiveness, throughput and flexibility. Furthermore, event-driven programming separates event-processing logic from the rest of a program’s code.
Core concepts: An event handler is a callback function that will be called when an even is triggered. A Main Event Loop continuously listens for event triggers and calls the associated event handler for that event.
There is, at most, one event handler running at any given time and any event handler will run and complete its task without any interruption.
Event listeners can be removed for performance reasons, to avoid memory leeks, or if an event listener is no longer needed. removeListener
and removeAllListeners
are two class methods that remove either singular listeners are all listeners collectively.
Node.js uses events heavily and it is also one of the reasons why Node.js is pretty fast compared to other similar technologies. As soon as the Node server starts, it simply initiates its variables, declares functions and then simply waits for the event to occur.
*image sourced from: https://www.tutorialspoint.com/nodejs/nodejs_event_loop.htm