Asynchronous vs Synchronous

Asynchronous vs Synchronous

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.

No alt text provided for this image

The following program demostrates the asynchronous nature of Node.js

Drawbacks of Asynchronous Programming

No alt text provided for this image

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

No alt text provided for this image

The above program demostrates how promise overcomes the drawbacks of asynchronous programming.

要查看或添加评论,请登录

NIRBHAY MAITRA的更多文章

社区洞察

其他会员也浏览了