-
Notifications
You must be signed in to change notification settings - Fork 7
Event Driven Applications
-
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
-
If we want to break that flow and switch to asynchronous mode then we can use setImmediate() or process.nextTick() methods
-
Event-Driven Programming is a logical pattern that we can choose to confine our programming within to avoid issues of complexity and collision
-
Every time you interact with a webpage through it’s user interface, an event is happening
- These events have associated functions that, when triggered, are executed to make a change to the user interface in some way
-
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
-
Node.js natively provides us with a useful module called EventEmitter that allows us to get started incorporating Event-Driven Programming in our project right away
const EventEmitter = require('events').EventEmitter; const myEventEmitter = new EventEmitter; -
To remove event listeners in EventEmitter we can use the removeListener or removeAllListeners method