DEV Community

Cover image for Difference in Asynchronous & Synchronous JavaScript Code
Mandeep Singh Panwar
Mandeep Singh Panwar

Posted on

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 ProgrammingSynchronous 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 ProgrammingAsynchronous 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 💙👨‍💻

Top comments (1)

Collapse
 
alifakoor profile image
Ali Fakoor

this is so clean, thank you