Synchronous, Asynchronous Operations and Promises

Synchronous, Asynchronous Operations and Promises

In this article, you will learn about what are synchronous operations, asynchronous operations, and promises.

Table Of Contents

  1. What are Synchronous Operations?
  2. What are Asynchronous Operations?
  3. What are Promises?
  4. Conclusion

What are Synchronous Operations?

Synchronous refers to real-time communication where each party receives messages instantly and both of them present at the same time.

Many programming commands are also synchronous — for example when you type in a calculation, the environment will return the result, to you instantly unless you program it not to.

In a human example, during a telephone call, you tend to respond to another person immediately.

Or, when a person visits a shop to buy something he waits for the shopkeeper until he brings the desired result and hands it over to him.

sync.jpeg

What are Asynchronous Operations?

Asynchronous refers to non-real-time communication where two or more people communicate without having to be “present” at the same time

In a human example, when a person visits the shop to buy something and the shopkeeper gave him the promise of getting the thing after some time and the person leaves and when the promises get fulfilled he receives his item.

Programatically, we can say in async a user requests for some data through the server, then it runs under the hood and he can continue his work instead of waiting for the response. After some time, he receives his response in the form of a promise.

async.jpeg

What are Promises?

Promises are the mechanism by which javascript implements & provides async functionality and its objects represent the completion of an async operation and it returns a response (whether the request is fulfilled or rejected).

Promises have three states of response -

  • pending
  • fulfilled
  • rejected

image.png

Conclusions

Synchronous requests often cause hangs on the web. But developers typically don't notice the problem because the hang only manifests with poor network conditions or when the remote server is slow to respond. The recommendation is that developers move away from the synchronous API and instead use asynchronous requests.

Thank you for reading!