Asynchronous vs Synchronous
NIRBHAY MAITRA
Full-stack Developer at Mobius by Gaian with expertise in React.js, Node.js and MongoDB
In Synchronous operations tasks are performed one at a time whereas in Asynchronous, second tasks do not wait for the first task. Thats is why Asynchronous are fast. Node.js and Javascript are Asynchronous while php is synchronous.
The following program demostrates the asynchronous nature of Node.js
Drawbacks of Asynchronous Programming
So from the above demo we can clearly see that the new value of b is never read. Instead the other process got executed first. This is the major drawback when it comes to asynchronous programming. To overcome this in Node.js we use Promise, async await and callback functions
Handling Asynchronous data in Node.js
Promise:- A Promise in Node means?an action which will either be completed or rejected. In case of completion, the promise is kept and otherwise, the promise is broken. So as the word suggests either the promise is kept or it is broken. And unlike callbacks, promises can be chained
The above program demostrates how promise overcomes the drawbacks of asynchronous programming.