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 🔗
Top comments (1)
this is so clean, thank you