Asynchronous Programming And Node JS Promises


As you know the new ECMAScript versions are asynchronous. Because JavaScript is based on ECMAScript, now JavaScript is also asynchronous and so is NodeJS. So if you need to develop an app in NodeJS, you should have a knowledge to handle the asynchronism. Hope this article will explain you the difference between asynchronous and synchronous programming clearly. And also how to handle asynchronism with callback functions.

Synchronous vs Asynchronous


Non blocking code do not prevent the execution of piece of code. In general if we execute in Synchronous manner (one after another), we unnecessarily stop the execution of those code which is not depended on the one you are executing.
Asynchronous does exactly opposite, asynchronous code executes without having any dependency and no order. This improves the system efficiency and throughput. Asynchronous programming is great for faster execution of programs.

Let's take the example below if you couldn't get it.
If you execute the above JavaScript code, the output will be like below because JS is asynchronous.
i am first
i am third
i am second

But if JavaScript is synchronous, the output will be like below.
i am first
i am second
i am third

I think now you can understand the difference between synchronous and asynchronous programming.

NodeJS CallBacks


A traditional method of handling the asynchronism of JS is to use JavaScript callback functions. In general definition, callbacks are just functions that pass to other functions. I know that doesn't make any sense. I'm not going to explain more about callback functions in this article because we are going to use NodeJS promises to handle the asynchronism very nicely :) (Actually you need callbacks to use promises. But here you don't need to learn callbacks to understand the rest)

NodeJS Promises


i am first
i am second
i am third
If you really need to get the above output for the above mentioned JavaScript code, you have to use JavaScript promises. Simply if you need to execute, compose, and manage asynchronous operations, you can use promises. They also allow you to handle asynchronous errors using approaches that are similar to synchronous try/catch.

A promise is an object which can be returned synchronously from an asynchronous function. It will be in one of 3 possible states:
  1. Fulfilled: resolve() will be used to indicate
  2. Rejected: reject() will be used to indicate
  3. Pending: not yet fulfilled or rejected
I know it's little bit confusing for beginners. Lets do some coding to get the above output which is in the correct order.
Just clearly look at the second function and the last 4 lines. Compare this code with the above code
and look how we defined a "Promise" and how we used it with the keyword "then". In 6th line, we created a new promise and we resolve it after printing "i am second". In last 4 lines, we waited until it resolves the promise using "then" keyword and called for the third() function.

Ok... That's not all about NodeJS or even promises and callbacks. You have to read other online articles and follow tutorials with some of your own practices to learn more about promises and callbacks.

Good luck for your next NodeJS project. Please leave your comments below.