๐๐ฎ๐ ๐ฎ๐ณ ๐ผ๐ณ ๐๐ฒ๐ฎ๐ฟ๐ป๐ถ๐ป๐ด ๐ ๐๐ฅ๐ก ๐ฆ๐๐ฎ๐ฐ๐ธ
I studied Asynchronous JavaScript today.
Until now, my code ran sequentially. One line finished before the next line started. This approach blocks the browser during heavy tasks.
I learned how JavaScript manages time-consuming tasks like API calls or database queries.
Synchronous vs. Asynchronous:
- Synchronous code runs line by line. One slow task stops everything.
- Asynchronous code runs tasks in the background. The rest of your script keeps running.
I practiced with the setTimeout utility. I set a delay in milliseconds. I noticed that code written after a timeout runs before the timeout finishes. This shows how non-blocking code works.
I also encountered Callback Hell.
Callbacks are functions passed as arguments. When you nest many callbacks to handle sequential tasks, the code becomes a mess. It is hard to read and hard to debug.
To understand this, I built a simulation:
- I wrote a script to mimic fetching user data.
- I nested multiple data fetches (Data 1 to Data 2 to Data 3).
- Each fetch waited for the previous one to finish.
The resulting structure looked like a pyramid. This messy code shows why developers moved to better tools.
Tomorrow, I move to the next step:
- Promises with .then() and .catch().
- Async and Await syntax.
Did nested callbacks confuse you when you first learned them? Do you still see old callback code in professional projects?
Source: https://dev.to/ali_hamza_589ec7b3eb6688d/day-27-of-learning-mern-stack-1cgj