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
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
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
A promise is an object which can be returned synchronously from an asynchronous function. It will be in one of 3 possible states:
- Fulfilled: resolve() will be used to indicate
- Rejected: reject() will be used to indicate
- 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
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.
4 Comments
Nice article.
ReplyDeleteKeep it up 🙌
Thanks for the comment and the encouragement
DeleteThis comment has been removed by the author.
ReplyDeleteGreat Info, Thanks For Sharing , keep it up we are here to learn more
ReplyDeleteGreat! I like to share it with all my friends and hope they will also like this information.
Tableau Training In Hyderabad
Tableau Online Training
Tableau Training
Tableau Training In Ameerpet
Tableau Training Online
Post a Comment