0
0
Javascriptprogramming~3 mins

Why promises are used in Javascript - The Real Reasons

Choose your learning style9 modes available
The Big Idea

What if your app could keep working smoothly while waiting for slow tasks to finish?

The Scenario

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.

The Problem

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.

The Solution

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.

Before vs After
Before
const data = fetchData();
console.log(data); // might be undefined or cause errors
After
fetchData().then(data => {
  console.log(data);
});
What It Enables

Promises make it easy to handle tasks that take time, so your app stays fast and responsive.

Real Life Example

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.

Key Takeaways

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.