lookihe.blogg.se

Nodejs eventemitter
Nodejs eventemitter














Much of the NodeJS core API is built around an idiomatic asynchronous event-driven architecture, note that NodeJS also allows us to spin up multiple child process and worker threads, we are not going to dive into much detail about asynchronous programming in JavaScript but let's get a high level overview of how the event listener works.Īll synchronous code gets executed in the call stack, you know in LIFO (Last-in-first-out) manner, event driven code do not follow this manner, the function attached to the event listener is added to the Event's list of internal listeners, when the event is fired, the function attached to the listener is pushed on to the call stack.

nodejs eventemitter

In NodeJS there is an event loop which is the name for the data structure that is the Event listener, this allows NodeJs to handle multiple concurrent request while running on a single thread, allowing synchronous code to behave in an asynchronous manner. There is an event listener that sits and waits for events to occur which it will listen to, we can bind code to the listener thus whenever the event listener subscribes to an event, if a callback function is passed to the listener, the callback code will be called.

#NODEJS EVENTEMITTER HOW TO#

We are going to explore how to leverage the built in Event Emitter in Node js to write event driven systems, let's dive in.Įvent driven architecture is a programming paragdim where the control flow of a program is determined by a sequence of events. The API for interacting with the Event Emitter is nothing short of straight forward, we can see the benefit of this style by writing code that will only trigger when a certain event happens, just like the sprinklers turn on when the fire alarm goes off. NodeJS has a built in event Emitter which we can inherit in our classes to build an event driven architecture.

nodejs eventemitter nodejs eventemitter

Node JS, a JavaScript runtime that is built on top of the V8 JavaScript engine, the same one that powers Google's Chromium browsers.














Nodejs eventemitter