Menu Close

Synchronous and Asynchronous in JavaScript

In this article we will learn about Synchronous and Asynchronous in JavaScript. Here we will discuss where and why it required to use synchronous and asynchronous programming in JavaScript. To give you an idea, Synchronous is a blocking operation, whereas Asynchronous is a non-blocking operation, which means that in synchronous, another set of operations will be blocked until a specific operation is completed, whereas in asynchronous, none of the operations are blocked. Let us now go through this in further depth to better grasp synchronous and asynchronous programming in JavaScript. Please read my previous article of Most popular ASP.Net Core Libraries every developer should know.

Synchronous in JavaScript

As the name implies, synchronous means in a sequential order, implying that each statement of the code is executed one by one. So, effectively, a statement must wait for the previous statement to be performed before proceeding.
Let us look at an example to better grasp this.

<script>
	document.write("This is my first statement"); // First
	
	document.write("This is my second statement") ;// Second
		
	document.write("This is my third statement"); // Third

</script>

Output

Now if we try to understand the order of execution here by seeing the output is sequential, I mean the output is performed in a particular sequence.

This is my first statement
This is my second statement
This is my third statement

Asynchronous in JavaScript

Asynchronous code permits the programm to be executed immediately, but synchronous code prevents further execution of the remaining code until the current one is completed. This may not appear to be a major issue, but when viewed in context, it can result in the User Interface being delayed.

Let’s look at an example of how Asynchronous JavaScript works.

<script>
	document.write("This is my first statement"); // First

	setTimeout(() => {
	   document.write("This is my second statement") ;// Second
  }, 2000);
		
	document.write("This is my third statement"); // Third

</script>

Output

Here , instead of executing the setTimeout function, the code first logs in “This is my first statement”, then “This is my third statement”, and then runs the setTimeout function and it print “This is my second statement”.

As usual, the first statement was logged in first. Because we execute JavaScript in browsers, there exist web APIs that manage these things for users. So, what JavaScript does is call the setTimeout function in such a web API, and then we continue to run our code as usual. So it does not prevent the rest of the code from executing, and when all of the code has completed, it is pushed to the call stack and eventually executed. This is how asynchronous JavaScript works.

This is my first statement

This is my third statement

This is my second statement  // setTimeOut() print here

Difference between Synchronous vs Asynchronous?

Now let us know the difference between synchronous and asynchronous programming.

Synchronous programming

  • Sequential Execution: In synchronous programming, the code is executed sequentially, which means one by one, so one statement must be completed before moving on to the next statement.
  • Blocking Nature: The synchronous operation blocks the main execution thread until they are completed. 
  • Simple Control Flow: The synchronous code flows are predictable and straightforward, and it makes it easy to understand the execution order of a program. 

Asynchronous programming

  • Concurrent Execution: In asynchronous programming, the code is executed concurrently even if the current code is running; other codes are allowed to run parallelly even without waiting for the current code to finish.
  • Non-Blocking Nature: Operations are nonblocking in nature as they don’t block the execution flow of the program. When a particular operation is started, the program can continue to execute other code or the statement, and a callback function, promise, or async/await syntax is required to handle the result or continuation of the operation.
  • Event-driven Model: Asynchronous programming most often relies on event-driven models, which means functions are invoked or triggered automatically in response to a particular event or whenever the asynchronous operations are completed, and this makes the model responsive

Conclusion

In this article we discussed about Synchronous and Asynchronous in JavaScript. Here we will discuss where and why it required to use synchronous and asynchronous programming in JavaScript.

Leave behind your valuable queries and suggestions in the comment section below. Also, if you think this article helps you, do not forget to share this with your developer community. Happy Coding 🙂

Related Articles

SUPPORT ME

Buy Me A Coffee

Leave a Reply

Your email address will not be published. Required fields are marked *