Difference in Asynchronous & Synchronous JavaScript Code
Mandeep Singh Panwar
Posted on June 30, 2020
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 for200ms
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 ππ¨βπ»
π πͺ π
π©
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.