Placeholder canvas

Important JavaScript concepts every NodeJS Programmer should know

Anurag Rath

Friday Mar 15

NodeJS is a superstar framework as we call it. Many of you would be interested to know what are those key concepts one should to be aware of if you are preparing for NodeJS learning path. Interesting fact is that javascript fundamentals plays a vital role in helping you make the learning path easy since NodeJs uses javascript on the server side. 

We, at ikigaiHub, have curated you few important concepts for you to take the plunge & we hope you gain from it.

Non-blocking or Asynchronous I/O 

NodeJS operates in an asynchronous I/O. This means, NodeJS which is a server side framework do not keep the client (browser) waiting until the response is sent for the previous request. It instead starts processing all the incoming requests asynchronously putting the processing/ unfinished requests in event loop while serving other requests. This is one of the important feature which has a good impact on performance when using NodeJS.

Prototypes

If you are familiar to object oriented concept called inheritance, understanding prototype becomes easier. Prototype in NodeJS is nothing but inheritance with the only difference of doing it via objects. In other programming languages inheritance happens via classes. While you inherit a class and create objects to do code reuse, here in NodeJS you would create an object, build its properties and then you inherit from the object to achieve inheritance. This method of inheritance in NodeJS is called Prototypal Inheritance. Every object is internally linked to Object.prototype. The look up for a property value goes in a chain like structure until the property is defined in either the object itself or in its parent object. This look up happens until Object.prototype is reached and if the property is not found here too, the result would be undefined. 

Modules

Modules are code grouped logically which performs a specific action. There are majorly two kinds of modules — user defined and core modules. 

Core modules are something which readily comes with NodeJS library. These modules are usually a general purpose functions wrapped as modules which is used very often by most of the developer. 

User Defined Modules are custom made modules defined by self as per the need of the application being built. One opts for this, when the desired feature is not completely achievable by the core modules present.

 Callbacks

 A callback is a function that is passed into another function as an argument to be executed later. The name itself denotes that you are calling a function. When and how is what we will describe now. 

In Javascript, functions are first-class objects. 

A first class object is an entity that can be dynamically created, destroyed, passed to a function, returned as a value, and have all the rights as other variables in the programming language have.

You can pass functions to other functions as variables. A function can be passed as a parameter to another JavaScript function, and the callback function is run inside of the function it was passed into. 

Promises

Promise — Think what does this mean in real life? It’s nothing but you give a word of doing something to someone irrespective of the outcome it can produce. If you are able to keep the word, there is a positive outcome however in case of not being able to keep the promise you would have a reason as to why you couldn’t keep it. 

In programming world of Javascript, that is exactly what a promise is. Promise which is a special javascript object which can be created and an executor function will be passed to it. 

ex : new Promise( /* executor */ function(resolve, reject) { ... } );

The executor function runs automatically and once it finishes the job, it would call the functions that is set as its parameters.

resolve — Denotes the task is done successfully. As a result, it 

  • sets state to "fulfilled",
  • sets result to value.

reject — Denotes the task failed for some reason. As a result, it 

  • sets state to "rejected",
  • sets result to error

Once you set the promise, based on the status of promise consuming functions can be called which will perform the relevant actions. Consumer functions are as follows 

then() — Can be invoked when the resolve function is called denoting that executor function has successfully obtained the result and now based on success we need to execute further steps. 

catch() — can be invoked when you want to handle the reject function of a promise and log/ handle error scenario

finally() — This would function very similar to try, catch and finally where you would want to handle the final set of actions that you want to take care. 

Observables

Before understanding about observables, let’s know what is observer design pattern.

The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.

Observables are functions which throws values. Objects called “observers” subscribe to these values and observables achieves tremendous power of async execution through this pub-sub model. Pub means “publisher” which is observable function where as sub mean subscribers which are observable objects.

In analogy to real life, this is more like self service restaurant. Self service is the observable function, customer with a certain token number is the observer. Once the dish is ready, the token number will be called to denote that food is ready. 

In case of observable, it’s always calls when the value is ready to be served and all the observers just listen. 

There are much more interesting concepts which makes Javascript a fun learning language and also which makes NodeJs what it is.

To take a dive into such concepts and learn them through industry specialists as your mentors, participate in our upcoming NodeJs 101 bootcamp and be the champ ! 

Till then Happy Coding !