DEV Community

Cover image for Understanding Promises in JavaScript
Pantelis Theodosiou
Pantelis Theodosiou

Posted on • Updated on • Originally published at pantelis.theodosiou.me

Understanding Promises in JavaScript

Photo by Emre Turkan on Unspash

Introduction

Modern web development relies heavily on asynchronous programming, which enables us to handle time-consuming operations effectively without delaying the completion of other tasks. Promises are a strong feature of JavaScript that make asynchronous operations easier to understand and improve code readability. The goal of this article is to give readers a thorough understanding of JavaScript promises, including information on their definition, lifecycle stages, helper functions, and inner workings.

What are Promises in JavaScript?

An asynchronous operation's eventual success (or failure) and the value it generates are represented by an object called a promise. Instead of nesting callbacks, it enables us to handle asynchronous code in a more elegant and organized way by chaining methods.

Promises can be in one of three states: pending, fulfilled, or rejected. The initial state of a Promise, which denotes that the asynchronous operation is still ongoing, is pending. If the operation succeeds, the Promise moves to the fulfilled state, and if there is a problem, it switches to the rejected state. "Settling" describes the process of going from pending to either fulfilled or rejected.

Promise Lifecycle Stages

Let's examine each phase of the lifecycle of a Promise in depth, with code examples:

Pending Stage

A Promise is in the pending state when it is created. The asynchronous operation is still ongoing at this point, and the Promise is neither accepted nor rejected.

Here's an example:

const promise = new Promise((resolve, reject) => {
  // Asynchronous operation (e.g., fetching data from an API)
  // resolve(result) or reject(error) will be called later
});
Enter fullscreen mode Exit fullscreen mode

Fulfilled Stage

The Promise goes into the fulfilled state once the asynchronous operation is successfully completed. At this point, the associated value (result) becomes available. To handle a fulfilled Promise, we use the .then() method.

Here's an example:

const promise = new Promise((resolve, reject) => {
  // Simulating an asynchronous operation
  setTimeout(() => {
    resolve("Operation succeeded!");
  }, 2000);
});

promise.then((result) => {
  console.log(result); // Output: "Operation succeeded!"
});
Enter fullscreen mode Exit fullscreen mode

Rejected Stage

In the event that there is a problem with the asynchronous operation, the Promise goes into the rejected state. It denotes that the operation failed and provides an error object with pertinent information. To handle a rejected Promise, we use the .catch() method.

Here's an example:

const promise = new Promise((resolve, reject) => {
  // Simulating an asynchronous operation
  setTimeout(() => {
    reject(new Error("Something went wrong!"));
  }, 2000);
});

promise.catch((error) => {
  console.log(error.message); // Output: "Something went wrong!"
});
Enter fullscreen mode Exit fullscreen mode

Chaining Promises

Promises have a number of important benefits, including the ability to chain together several asynchronous operations, which improves code readability and prevents the dreaded "callback hell." We accomplish this by using the .then() method to return a new Promise.

Here's an example:

const getUser = () => {
  return new Promise((resolve, reject) => {
    // Simulating an asynchronous operation
    setTimeout(() => {
      resolve({ id: 1, name: "John" });
    }, 2000);
  });
};

const getUserPosts = (user) => {
  return new Promise((resolve, reject) => {
    // Simulating an asynchronous operation
    setTimeout(() => {
      resolve(["Post 1", "Post 2"]);
    }, 2000);
 });
};

getUser()
  .then((user) => getUserPosts(user))
  .then((posts) => console.log(posts)); // Output: ["Post 1", "Post 2"]
Enter fullscreen mode Exit fullscreen mode

Helper Functions for Promises

Along with the core methods that Promises provide, JavaScript also provides a number of helper functions that improve the functionality and readability of asynchronous code. Common tasks are made easier by these helper functions, which also improve control flow and error handling.

Promise.all()

When all of the Promises in the input array have been fulfilled, this function returns a new Promise that fulfills.

Here's an example:

const fetchUser = () => {
  return new Promise((resolve, reject) => {
    // Simulating an asynchronous API call to fetch user data
    setTimeout(() => {
      const user = { id: 1, name: "John" };
      resolve(user);
    }, 2000);
  });
};

const fetchPosts = () => {
  return new Promise((resolve, reject) => {
    // Simulating an asynchronous API call to fetch user posts
    setTimeout(() => {
      const posts = ["Post 1", "Post 2"];
      resolve(posts);
    }, 1500);
  });
};

const fetchComments = () => {
  return new Promise((resolve, reject) => {
    // Simulating an asynchronous API call to fetch user comments
    setTimeout(() => {
      const comments = ["Comment 1", "Comment 2"];
      resolve(comments);
    }, 1000);
  });
};

Promise.all([fetchUser(), fetchPosts(), fetchComments()])
  .then(([user, posts, comments]) => {
    console.log("User:", user);
    console.log("Posts:", posts);
    console.log("Comments:", comments);
  })
  .catch((error) => {
    console.log("Error:", error);
  });
Enter fullscreen mode Exit fullscreen mode

The three functions fetchUser(), fetchPosts(), and fetchComments() are included in the example above. For user data, user posts, and user comments, each function simulates an asynchronous API call by returning a Promise.

By passing an array of Promises ([fetchUser(), fetchPosts(), fetchComments()]) to Promise.all(), we create a new Promise that fulfills once every Promise in the array has been successfully completed. When handling the fulfillment, the .then() method applies an array destructuring syntax to retrieve the resolved values of each Promise.

When all of the Promises are fulfilled in this situation, the array destructuring assigns the resolved values of fetchUser(), fetchPosts(), and fetchComments() to the variables user, posts, and comments respectively. The user, posts, and comments are then recorded to the console.

The .catch() method is called and the error is logged to the console if any of the Promises fail.

Promise.all() allows us to efficiently fetch multiple asynchronous resources and handle them all at once after each request has successfully finished.

Promise.race()

As soon as any of the Promises in the input array settles, this function returns a new Promise that either fulfills or rejects.

Here's an example:

const fetchResource = (resource, delay) => {
  return new Promise((resolve, reject) => {
    // Simulating an asynchronous API call with a specified delay
    setTimeout(() => {
      resolve(`${resource} is fetched successfully in ${delay}ms`);
    }, delay);
  });
};

const resource1 = fetchResource("Resource 1", 2000);
const resource2 = fetchResource("Resource 2", 1500);
const resource3 = fetchResource("Resource 3", 1000);

Promise.race([resource1, resource2, resource3])
  .then((result) => {
    console.log(result);
  })
  .catch((error) => {
    console.log(error);
  });
Enter fullscreen mode Exit fullscreen mode

In the example above, there are three functions called fetchResource() that simulate asynchronous API calls by returning Promises. Each call simulates the time it takes to fetch a particular resource by having a different delay time.

When an array of Promises ([resource1, resource2, resource3]]) is passed to the Promise.race() method, a new Promise is created that settles (fulfills or rejects) in response to any Promise in the array. The resolved value of the successful Promise is passed as the result parameter and is logged to the console in the .then() method, which is used to handle fulfillment. In this case, the resource that resolves first (i.e., the one with the shortest delay) will be considered the winner, and its resolved value will be printed to the console.

The .catch() method is called and the error is logged to the console if any of the Promises fail.

We can run several asynchronous operations simultaneously and react to the outcome of the fastest one by using the Promise.race() method. It is beneficial in situations where we want to act in accordance with the initial response or completion.

Promise.resolve() and Promise.reject()

Without the need for additional asynchronous operations, these functions allow you to create Promises that have already been fulfilled or rejected, respectively.

Because of the strong capabilities these helper functions offer to manipulate and control Promises, JavaScript asynchronous programming is now more adaptable and expressive.

Promise.resolve() and Promise.reject() are used in the following example of code:

const fetchData = (shouldSucceed) => {
  if (shouldSucceed) {
    return Promise.resolve("Data fetched successfully");
  } else {
    return Promise.reject(new Error("Failed to fetch data"));
  }
};

fetchData(true)
  .then((result) => {
    console.log(result);
  })
  .catch((error) => {
    console.log(error);
  });

fetchData(false)
  .then((result) => {
    console.log(result);
  })
  .catch((error) => {
    console.log(error);
  });
Enter fullscreen mode Exit fullscreen mode

The fetchData() function in the example above has a shouldSucceed parameter that specifies whether the data fetch should be successful or unsuccessful. Promise.resolve() is used to create and return a Promise that immediately fulfills with the message "Data fetched successfully" if shouldSucceed is true. Promise.reject() is used to create and return a Promise that immediately rejects with a new Error object and the message "Failed to fetch data" if shouldSucceed is false.

The returned Promise is fulfilled in the first call to fetchData() with shouldSucceed set to true and the fulfillment is managed by the .then() method. The result parameter is passed the resolved value "Data fetched successfully", which is then logged to the console.

In the second invocation of fetchData() with shouldSucceed set to false, the returned Promise is rejected, and the .catch() method is used to handle the rejection. The error object containing the message "Failed to fetch data" is passed as the error parameter and logged to the console.

By utilizing Promise.resolve() and Promise.reject(), we can easily create Promises that are already resolved or rejected without the need for additional asynchronous operations. This is useful when handling synchronous values or errors as Promises.

Conclusion

JavaScript's asynchronous programming has been revolutionized by promises, which offer a well-organized and beautiful way to handle laborious tasks. Promises help us avoid callback hell and create more readable, maintainable code.

The definition of promises, their lifecycle stages, and the function of helper functions like Promise.all(), Promise.race(), Promise.resolve(), and Promise.reject() were all covered in this extensive guide. We can handle asynchronous operations efficiently and gracefully handle success and error scenarios by understanding the lifecycle stages - pending, fulfilled, and rejected.

Developers can create dependable and effective applications that easily handle complex asynchronous tasks by utilizing Promises and their helper functions. Control flow is streamlined and code readability is improved by Promise chains. Additionally, JavaScript's helper functions improve error handling and streamline the execution of numerous asynchronous operations.

Modern web development requires the use of asynchronous programming, and becoming proficient with promises enables developers to produce code that is cleaner and easier to maintain. You can build robust applications that effectively handle asynchronous operations by integrating Promises into your JavaScript projects and utilizing their strength and helper functions.


If you found this post helpful or enjoyed it, consider supporting me by buying me a coffee. Your support helps me create more valuable content. ☕ Thank you!

Top comments (6)

Collapse
 
grantdotdev profile image
Grant Riordan

Great article, explains the use of promises well.

Collapse
 
ptheodosiou profile image
Pantelis Theodosiou • Edited

Thank you! Glad you found it helpful!

Collapse
 
grantdotdev profile image
Grant Riordan

Apologies, I didn't utilise the content of the article myself. I am already familiar with the concept / usage.

I was more congratulating you on the well written explanation 👍 good job.

Collapse
 
felistus profile image
Obieze Ezeugo Felistus

Nice article. Good job!

Collapse
 
dyaskur profile image
Muhammad Dyas Yaskur

Thanks to your article. Even though I had previously utilized JavaScript promises without a comprehensive understanding, your article has greatly enhanced my comprehension.

Collapse
 
ptheodosiou profile image
Pantelis Theodosiou

I'm happy to hear that my article helped you understand JavaScript promises better. Please feel free to ask any further questions you may have.