Avatar

Sourav Dubey

Developer

Read Resume

Understanding JavaScript Promises β€” Part 2

thumbnail

JavaScript Promise Static Methods πŸš€

πŸ‘‰ Refer to Part 1 where I discussed the basics of Promises.

JavaScript promises have transformed asynchronous programming by making it more manageable and structured.
Alongside instance methods, JavaScript also provides a set of static methods that make working with promises even more powerful.

In this post, we’ll explore the 6 most commonly used static methods of the Promise class, with examples.


1. Promise.all

Promise.all takes an iterable of promises and returns a new promise.

  • Resolves β†’ when all promises fulfill (returns an array of results).
  • Rejects β†’ immediately if any one promise rejects.

βœ… Use it when multiple async tasks can run independently but you need all results.

const promise1 = new Promise((resolve) => {
  setTimeout(() => {
    resolve("First value");
  }, 2000);
});

const promise2 = new Promise((resolve) => {
  setTimeout(() => {
    resolve("Second value");
  }, 3000);
});

Promise.all([promise1, promise2])
  .then((results) => {
    console.log(results); // ["First value", "Second value"]
  })
  .catch((error) => {
    console.log(error); // If any promise rejects
  });

2. Promise.allSettled

Promise.allSettled waits for all promises to settle (fulfilled or rejected). The result is an array of objects describing each promise outcome.

const promise1 = Promise.resolve("Fulfilled value");
const promise2 = Promise.reject(new Error("Rejected reason"));
const promise3 = Promise.resolve("Another fulfilled value");

Promise.allSettled([promise1, promise2, promise3]).then((results) => {
  console.log(results);
});

Output

[
  { status: "fulfilled", value: "Fulfilled value" },
  { status: "rejected", reason: Error("Rejected reason") },
  { status: "fulfilled", value: "Another fulfilled value" }
]

3. Promise.any

Promise.any resolves as soon as the first promise fulfills.

  • If all reject, it rejects with an AggregateError.

Great when you just need the first successful result.

const promise1 = Promise.reject(new Error("1st promise rejected"));
const promise2 = Promise.resolve("Successful value");
const promise3 = Promise.reject(new Error("2nd promise rejected"));

Promise.any([promise1, promise2, promise3])
  .then((result) => {
    console.log(result); // "Successful value"
  })
  .catch((error) => {
    console.log(error); // AggregateError if all fail
  });

4. Promise.race

Promise.race settles as soon as any promise resolves or rejects. It adopts the state of the first settled promise.

const promise1 = new Promise((resolve) => {
  setTimeout(() => resolve("First value"), 2000);
});

const promise2 = new Promise((_, reject) => {
  setTimeout(() => reject(new Error("Second promise rejected")), 3000);
});

Promise.race([promise1, promise2])
  .then((result) => {
    console.log(result); // "First value"
  })
  .catch((error) => {
    console.log(error); // If the first settled one rejects
  });

5 & 6. Promise.resolve & Promise.reject

Both also exist as static methods.

-- Promise.resolve β†’ creates an immediately fulfilled promise. -- Promise.reject β†’ creates an immediately rejected promise.

const resolvedPromise = Promise.resolve("Resolved value");
resolvedPromise.then((result) => console.log(result)); // "Resolved value"

const rejectedPromise = Promise.reject(new Error("Rejected reason"));
rejectedPromise.catch((error) => console.log(error)); // "Rejected reason"

🎯 Conclusion

JavaScript’s static promise methods give developers powerful tools to manage async workflows:

βœ… Promise.all β†’ wait for all to succeed

βœ… Promise.allSettled β†’ wait for all to settle

βœ… Promise.any β†’ first success wins

βœ… Promise.race β†’ first completion (success or fail)

βœ… Promise.resolve / Promise.reject β†’ quickly create settled promises

Mastering these will help you write cleaner, more efficient async code.

πŸ”₯ Use them to orchestrate complex workflows and make your JavaScript code shine!