Skip to main content
JavaScript Promises Explained with Simple Examples
0

JavaScript Promises Explained with Simple Examples

Learn JavaScript Promises with simple examples. Understand Promise states, then(), catch(), finally(), Promise.all(), and async/await.

Read in:

JavaScript Promises Explained with Simple Examples

JavaScript is used to build interactive websites and web applications. Sometimes, we need to perform tasks that take time, such as fetching data from an API, loading user details, or sending information to a server.

These tasks do not finish immediately. This is where JavaScript Promises help us. Promises make it easier to handle asynchronous operations without making our code difficult to read.

In this article, we will understand JavaScript Promises, their states, and important methods with simple examples.

What Is a Promise in JavaScript?

A Promise in JavaScript is an object that represents the result of an asynchronous operation.

In simple words, a Promise tells us that a task will finish in the future. It can either be successful or fail.

For example, when you order food online, you wait for the order to be delivered. The order may be completed or cancelled. A Promise works in a similar way.

What Are the Three States of a Promise?

Every JavaScript Promise has three states:

  1. Pending: The operation is still running.
  2. Fulfilled: The operation completed successfully.
  3. Rejected: The operation failed.

Once a Promise becomes fulfilled or rejected, its state cannot change again.

How to Create a Promise in JavaScript?

We can create a Promise using the new Promise() constructor. It takes a function with two parameters: resolve and reject.

const myPromise = new Promise((resolve, reject) => {
    let success = true;
    if (success) {
        resolve("Task completed successfully");
    } else {
        reject("Something went wrong");
    }
});

Here:

  • resolve() is called when the task is successful.
  • reject() is called when the task fails.

How to Use then() and catch()?

Two important Promise methods are .then() and .catch().

  • then(): Handles the successful result.
  • catch(): Handles errors when a Promise is rejected.

Example

const myPromise = new Promise((resolve, reject) => {
    resolve("Data loaded successfully");
});
myPromise
    .then(result => {
        console.log(result);
    })
    .catch(error => {
        console.log(error);
    });

Output:

Data loaded successfully

If the Promise is rejected, the .catch() method handles the error instead.

What is finally() in JavaScript?

The finally() method runs after a Promise is settled, whether it succeeds or fails.

myPromise
    .then(result => console.log(result))
    .catch(error => console.log(error))
    .finally(() => console.log("Task completed"));

It is useful for tasks like hiding a loading spinner or cleaning up resources.

What Is Promise Chaining?

Promise chaining means connecting multiple .then() methods to execute tasks one after another.

Promise.resolve(10)
    .then(result => {
        return result * 2;
    })
    .then(result => {
        return result + 5;
    })
    .then(result => {
        console.log(result);
    });

Output:

25

Each .then() receives the result returned by the previous .then().

Important Promise Methods in JavaScript

JavaScript provides some useful methods to work with multiple Promises.

1. Promise.all()

Waits for all Promises to fulfill. If one Promise rejects, the returned Promise also rejects.

Promise.all([
    Promise.resolve("User Data"),
    Promise.resolve("Product Data")
]).then(result => {
    console.log(result);
});

2. Promise.allSettled()

Waits for all Promises to finish, whether they succeed or fail.

3. Promise.race()

Returns the result of the first Promise that settles, whether fulfilled or rejected. These methods are useful when working with multiple API requests.

Promises vs Async/Await

Async/await is another way to handle asynchronous operations in JavaScript. It is built on top of Promises.

Example using async/await:

async function getData() {
    const result = await Promise.resolve("Data received");
    console.log(result);
}
getData();

Both Promises and async/await help us handle asynchronous tasks. However, async/await often makes the code easier to read.

Common Mistakes Beginners Make

Here are a few common mistakes to avoid when working with Promises:

  • Forgetting to use .catch() for error handling.
  • Forgetting to return a value inside .then() when the next step needs it.
  • Thinking that Promise callbacks run like normal synchronous code.
  • Not understanding the difference between fulfilled and rejected states.

Conclusion

JavaScript Promises make it easier to handle asynchronous operations such as API requests and loading data.

In this article, we learned how to create Promises, use resolve() and reject(), handle results with .then() and .catch(), and work with methods like Promise.all().

If you are learning JavaScript, practice these examples to understand how Promises work. They will also help you learn async/await and work with APIs in real-world projects.

📂 Categories

🏷️ Tags

Frequently Asked Questions

What is a Promise in JavaScript?

A Promise is an object that represents the future result of an asynchronous operation.

What are the three states of a Promise?

Pending, fulfilled, and rejected.

What is the difference between resolve() and reject()?

resolve() handles successful completion, while reject() handles failure.

What is the difference between then() and catch()?

.then() handles successful results, while .catch() handles errors.

Are async/await and Promises the same?

Async/await is built on Promises and provides a simpler way to write asynchronous code.

Version History 3 updates
  1. Oct 31, 2026

    Updated: title, description, content, categories, tags, featuredImage, images, status, faqs, seriesName, seriesOrder, wordCount, readingTimeMinutes, slug, rejectionReason

  2. Oct 31, 2026

    Updated: title, description, content, categories, tags, featuredImage, images, status, faqs, seriesName, seriesOrder, wordCount, readingTimeMinutes, slug, rejectionReason

  3. Oct 31, 2026

    Updated: title, description, content, categories, tags, featuredImage, images, status, faqs, seriesName, seriesOrder, wordCount, readingTimeMinutes, slug, rejectionReason

Discussion