What if your app could keep working smoothly while waiting for slow tasks to finish?
Why promises are used in Javascript - The Real Reasons
Imagine you want to fetch data from the internet and then show it on your webpage. If you try to do this step by step without any special tools, your program might freeze or get stuck waiting for the data to arrive.
Doing things one after another without waiting properly can make your app slow or unresponsive. You might also get confused about when the data is ready, causing errors or broken pages.
Promises let your program ask for data and keep working while waiting. When the data arrives, the promise tells your program to continue, making everything smooth and easy to manage.
const data = fetchData();
console.log(data); // might be undefined or cause errorsfetchData().then(data => {
console.log(data);
});Promises make it easy to handle tasks that take time, so your app stays fast and responsive.
When you order food online, you don't wait by the phone doing nothing. You do other things until the delivery arrives. Promises work the same way in code.
Manual waiting can freeze your app and cause errors.
Promises let your code keep running while waiting for results.
This makes apps faster, smoother, and easier to write.