๐๐ฎ๐ ๐ฎ๐ด ๐ผ๐ณ ๐๐ฒ๐ฎ๐ฟ๐ป๐ถ๐ป๐ด ๐ ๐๐ฅ๐ก ๐ฆ๐๐ฎ๐ฐ๐ธ
I completed four weeks of consistent learning today.
I moved past callback hell. I mastered JavaScript Promises.
Callbacks make code hard to read when you nest them. Promises solve this problem. A promise is an object. It represents the result of an asynchronous task.
A promise has three states:
โข Pending: The task is still running. โข Fulfilled: The task finished successfully. โข Rejected: The task failed.
You use two main methods to handle these states:
โข .then(): This runs when the promise succeeds. โข .catch(): This runs when the promise fails.
In real apps, you often consume promises from APIs.
The best part of today was learning promise chaining. You return a new promise inside a .then() block. You link the next .then() directly below it. This turns messy nested code into a clean vertical line.
Example:
getData(1) .then((res) => getData(2)) .then((res) => getData(3)) .then((res) => console.log("All data loaded!"));
This structure keeps your code readable and organized.
Source: https://dev.to/ali_hamza_589ec7b3eb6688d/day-28-of-learning-mern-stack-3h8m