Last Sync: 2022-07-20 19:30:04

This commit is contained in:
tactonbishop 2022-07-20 19:30:04 +01:00
parent 61989d4a87
commit 95be1ee539
2 changed files with 6 additions and 4 deletions

View file

@ -5,10 +5,12 @@ tags:
- node-js
- node-modules
---
# `events` module
In most case you won't interact with the `events` module directly since other modules and third-party modules are abstractions on top of this base-layer. For instance the `http` module is using events under the hood to handle requests and responses.
* Much of the NodeJS core is built around an [asynchronous event-driven architecture](Single-threaded%20asynchronous%20architecture.md) in which certain kinds of objects (called "emitters") emit named events that cause `Function` objects ("listeners") to be called.
* For example: a `fs.ReadStream` emits an event when the file is opened
Another way of putting this is to say that all events in Node inherit from the `EventEmitter` constructor, which is the class you instantiate to create a new event. At bottom everything in Node is an event with a callback, created via event emitters.
Because Node's runtime is [event-driven](/Programming_Languages/NodeJS/Architecture/Event_loop.md), it is event-emitter cycles that are being processed by the Event Loop, although you may know them as `fs` or `http` (etc) events. The call stack that the Event Loop works through is just a series of event emissions and their associated callbacks.
## Event Emitters
* All objects that emit events are instances of the `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.

View file

@ -1,6 +1,6 @@
h1, h2, h3, h4, h5, h6, p {
/* h1, h2, h3, h4, h5, h6, p {
font-family: 'Inter';
}
} */
pre, code {
font-family: 'JetBrains Mono' !important;