JavaScript: Synchronous or Asynchronous?
Anandteerth Onkar
Posted on August 27, 2021
Understanding the Synchronous and Asynchronous operations:
In synchronous operations, operation are performed one at a time. The next operation is unblocked only when the ongoing operation is completed.
Simple words, you need to wait for a operation in progress to finish, in order to start the next one in the queue.
In asynchronous operations, the next operation can be started before the previous one finishes.
Asynchronous programming allows to handling of multiple requests simultaneously, thereby allowing to accomplish more tasks within shorter time span.
So what is JavaScript?
At its core, JavaScript is by default Synchronous in nature.
Let's break it down:
It is Synchronous because it is Single-threaded. In single-thread one operation is executed at a time.
It blocks other operations in queue, as they need to wait until the previous operation has completed execution.
In nutshell:
In JavaScript, only one operation will be executed at a time and the successive operations in stack need to wait until the ongoing operation is completed.
Question: If JavaScript is Synchronous in nature, why on this earth Asynchronous JavaScript term so popular amongst JavaScript developers?
Answer: The developers manipulate JavaScript to behave Asynchronously. To make this manipulation, there are 3 techniques:
- Callback function
- Promises
- Async/Await.
Resources I recommend to understand these asynchronous techniques:
Callbacks:
Watch this awesome video by Philip Roberts while referring to his Demo website
Callback Hell: Callback Hell is a Nightmare. Refer this if you want to dig deeper into callbacks
Promises:
Video by Codevolution
Async/Await:
Best article on this topic by Mostafa Gaafar
All 3 techniques in one video by codeSTACKr
I will be creating articles on each of these topics. Need feedback from bloggers & readers of dev community, that will help me to improve my work.
Posted on August 27, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.