Introduction

In this article, we will discuss What Synchronous Programming is? What Asynchronous Programming is? And whether JavaScript is Synchronous or Asynchronous?

Many developers struggle to understand topics like Callbacks, Promises, and Async/Await, one of the reasons may be not understanding the actual need and core logic behind them.

Having a good grasp of this topic can help you understand them more easily.

Example

This can be better explained with the help of an example.

Imagine you gave your friend a set of tasks:

  • Get Video Games from adjacent towns (somewhere far).
  • Get Chocolate from a nearby store.

What your friend now does is, he first completes task one and head to an adjacent town, and let’s say that takes x amount of time. Then he comes back to you, gives you video games, and then goes to perform the second task, and let’s say it takes y amount of time. The total time taken is x+y. This was just a couple of tasks but imagine there are hundreds of them, the total time is taken increases rapidly if he is a single person doing all the tasks one by one in the order they are given.

The scenario above discussed is how JavaScript runs its code by default, it goes line by line performing each task one at a time before moving on to another one, which means that for the last task or command in the code to run, all the code above it should be executed first. JavaScript’s code is executed in a single thread and if a function was to take some time to finish it would freeze everything else in the meantime. Executing one thing at a time is nothing but Synchronous.

By default,** JavaScript is a synchronous, blocking, single-threaded language**. This simply means only one operation will be in progress at a time. Windows alert function is a good example like,alert("Hello World") when this function is executed whole web page is stuck and you can’t interact with it until you dismiss the alert.

#javascript #async #async asynchronous #javascript developers #synchronous #programming

Synchronous vs Asynchronous Programming
2.00 GEEK