Introduction
In the previous chapter, we talked about how event-driven programming is used in Node.js and how we can modify normal callback-based async operations to use async-await and promises. We know that the Node.js core API is built on async-driven architecture. Node.js has one event loop that does the processing for most async and event-based operations.
In JavaScript, the event loops run constantly and digest messages from the callback queue to make sure it is executing the right functions. Without events, we can see that the code is very deeply coupled. For a simple chatroom application, we would need to write something like this:
class Room { Â Â Â Â constructor() { Â Â Â Â Â Â Â Â this.users = []; Â Â Â Â } Â Â Â Â addUser(user) { Â Â Â Â Â Â Â Â this.users.push(user); Â Â Â Â } Â Â Â Â sendMessage(message) { Â Â &...