Repository navigation
Class 16
*Node.js core API is based on asynchronous event-driven architecture in which certain kind of objects called emitters periodically emit events that cause listener objects to be called. *All objects that emit events are members of EventEmitter class. These objects expose an eventEmitter.on() function that allows one or more functions to be attached to named events emitted by the object. *When the EventEmitter object emits an event, all of the functions attached to that specific event are called synchronously. All values returned by the called listeners are ignored and will be discarded.
*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: *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: *the nextTick is called in the iteration after the task of displaying 1st log statement is executed and then in the next cycle of picking up event from event loop task defined in process.nextTick() is called while setImmediate allows running some task asynchronously. *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:
https://medium.com/technoetics/node-js-event-emitter-explained-d4f7fd141a1a
*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.