Difference in Asynchronous & Synchronous JavaScript Code

mannypanwar

Mandeep Singh Panwar

Posted on June 30, 2020

Difference in Asynchronous & Synchronous JavaScript Code

Hello beautiful people on the internet πŸ™‹β€β™‚οΈ

This blog points out difference between Asynchronous & Synchronous JavaScript code

All developer eventually have to know about these two in order to write good code

Lets get to it then πŸš€

  • Synchronous Programming β–Ά Synchronous basically means that you can only execute one thing at a time
    • Like in JavaScript, the code runs from top to button executing single line of code at a time
  • Asynchronous Programming β–Ά Asynchronous means that you can execute multiple things at a time and you don't have to finish executing the current thing in order to move on to next one

Why does it even matter πŸ€”

Now that you know about this why does this even matter?

It is important because the code that might take more time (like API calls) must be written Asynchronously otherwise rest of the code will have to wait till the data is fetched.

In simple words πŸ’β€β™‚οΈ

  • If we make API calls or fetch data Synchronously, our code written after the call will have to wait till out call is made
  • Assuming that fetching data takes 200ms, JavaScript will wait for 200ms and then execute rest of your code.
  • While if the data fetching is written Asynchronously the 200ms wait is no longer there, the rest of the code runs without waiting for the data fetching making code run faster.

Now how to write code Asynchronously πŸ€”

There are various ways, most preferred are

  • promises
    • under this you fetch data inside a promise. Read more πŸ”—
  • async await
    • this is used to make normal function act Asynchronously. Read more πŸ”—

Thank you for reading πŸ’™πŸ‘¨β€πŸ’»

πŸ’– πŸ’ͺ πŸ™… 🚩
mannypanwar
Mandeep Singh Panwar

Posted on June 30, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related