Skip to main content

Command Palette

Search for a command to run...

32. ASYNC | Static Promise Methods - In depth

This article will talk about static promise methods in depth.

Updated
β€’7 min read
S
Full-Stack Developer with 4 years' experience, specializing in backend development. Skilled in JavaScript, React, Python, Databases, and AWS. Known for building scalable web apps, leading teams, and maintaining strong client communication. Upskilling in Generative AI.

Promise.all()

What it does?

  • Waits for all promises to resolve (fulfill) and returns the array of their results. If any one fails (reject), it becomes the error and all other results are ignored.

  • It takes an array (or iterable) of promises and returns a new Promise.

const newPromise = Promise.all(iterableOfPromises);

newPromise
.then()
.catch()
πŸ’‘
Promise.all() = "Run everything together, but give me the result only if everything succeeds."

Core behavior

1. Runs all promises in parallel

const p1 = fetch('/api/user');
const p2 = fetch('/api/posts');
const p3 = fetch('/api/comments');

Promise.all([p1, p2, p3])
  .then(([user, posts, comments]) => {
    console.log(user, posts, comments);
  });

πŸ‘‰ All 3 requests start at the same time, not one after another.

2. Resolves only when ALL succeed

The returned promise:

  • waits for every promise to resolve.

  • then gives results as an array (in order).

Promise.all([
  Promise.resolve(1),
  Promise.resolve(2),
  Promise.resolve(3)
])
.then(result => {
  console.log(result);
});

// Output: [1, 2, 3]

πŸ‘‰ Order is preserved, not based on completion time.

3. Fails fast (very important)

If any one promise rejects, the whole thing fails immediately.

Promise.all([
  Promise.resolve(1),
  Promise.resolve(3),
  Promise.reject("Error!")
])
.catch(err => {
  console.log(err);
});

// Output: "Error!"

πŸ‘‰ As soon as one fails: Promise.all() rejects, remaining results are ignored.

Real-world use cases

When you are dealing with multiple independent asynchronous operations together.

  • Fetching multiple APIs together.

  • Uploading multiple files.

  • Processing multiple images.

  • Running parallel DB queries.

When NOT to use Promise.all()

Avoid it when:

  • Tasks depend on each other.

  • You need partial results even if some fail.

  • You want to handle failures individually.

Important edge cases

1. Non-promises are allowed

Promise.all([1, 2, Promise.resolve(3)])
  .then(console.log); 

// Output: [1, 2, 3]

πŸ‘‰ Non-promises are treated as resolved values.

2. Empty array

Promise.all([]).then(console.log); // Output: []

πŸ‘‰ Resolves immediately.

πŸ”₯ Interview-level insight

The real power of Promise.all() is:

  • Parallelization β†’ performance improvement

  • Synchronization β†’ clean code structure


Promise.allSettled()

What it does?

  • Waits for all the promises to settle and returns their results as an array of objects with status and value.

  • It takes an array (or iterable) of promises and returns a new Promise.

const newPromise = Promise.allSettled(iterableOfPromises);

newPromise
.then()
.catch()
πŸ’‘
Promise.allSettled() = "Run everything, and tell me what happened to each one."

Core behavior

1. Runs all promises in parallel

const p1 = fetch('/api/user');
const p2 = fetch('/api/posts');
const p3 = fetch('/api/comments');

Promise.all([p1, p2, p3])
  .then(([user, posts, comments]) => {
    console.log(user, posts, comments);
  });

πŸ‘‰ All 3 requests start at the same time, not one after another.

2. Waits for ALL promises to settle

It waits until all promises get settle (resolve OR reject).

3. Returns structured results

Instead of raw values, we get array of objects describing each outcome:

Promise.allSettled([
  Promise.resolve(1),
  Promise.reject("Error!"),
  Promise.resolve(3)
])
.then((results) => {
  console.log(results);
});

/* Output:
[
  { status: "fulfilled", value: 1 },
  { status: "rejected", reason: "Error!" },
  { status: "fulfilled", value: 3 }
]
*/

Real-world use cases

When partial success is acceptable.

  1. Bulk operations (πŸ‘‰ Some may fail, but you still want full report)

    • Sending emails to many users

    • Processing batch jobs

    • Uploading multiple files

  2. Analytics / logging systems (πŸ‘‰ Some may fail, but you still want full report)

When NOT to use Promise.allSettled()

Avoid Promise.allSettled() when:

  • You need to stop immediately on failure.

  • Later operations depend on earlier success.

  • You don’t want to manually check statuses.

Important edge cases

1. Never rejects (almost always)

It always resolves, even if all promises fail. So, no need for .catch() (usually).

2. Order is preserved

Result order = input order (not completion order)

3. Non-promises are allowed

Promise.allSettled([1, Promise.reject("err")])

/* Output:
[
  { status: "fulfilled", value: 1 },
  { status: "rejected", reason: "err" }
]
*/

πŸ”₯ Interview-level insight

The real power of Promise.allSettled() is:

  • Resilience β†’ system doesn’t break on partial failure

  • Observability β†’ you get full insight into outcomes


Promise.race()

What it does?

  • Waits for the first promise to settle and its settled state (fulfill or reject) becomes the outcome.

  • It takes an array (or iterable) of promises and returns a new Promise.

const newPromise = Promise.race(iterableOfPromises);

newPromise
.then()
.catch()
πŸ’‘
Promise.race() = "Whichever finishes first, I go with that."

Core behavior

1. First promise to settle (fulfilled or rejected) wins

// fulfilled

Promise.race([
  new Promise(res => setTimeout(() => res("A"), 1000)),
  new Promise(res => setTimeout(() => res("B"), 500))
])
.then(result => {
  console.log(result);
});

// Output: "B"
// rejected

Promise.race([
  new Promise((_, rej) => setTimeout(() => rej("Error"), 500)),
  new Promise(res => setTimeout(() => res("Success"), 1000))
])
.catch(err => {
  console.log(err);
});

// Output: "Error"

Real-world use cases

  1. Request timeout mechanism.

  2. Fetching from the fastest server: When data is cached in multiple locations (e.g., different CDN nodes), Promise.race() selects the fastest source, reducing latency.

  3. Abandoning slow operations.

  4. Cancel-like behavior / Short-circuiting Promises.

Important edge cases

1. Empty array

Promise.race([]);

πŸ‘‰ Returns a Promise that never settles (⚠️ dangerous).

2. Non-promises

Promise.race([42, Promise.resolve(10)])
  .then(console.log); // Output: 42

πŸ‘‰ Non-promises are treated as already resolved. So, 42 wins instantly.

3. Other promises are NOT cancelled

Promise.race([p1, p2]);

πŸ‘‰ Even after one finishes, others continue running in background.

πŸ”₯ Interview-level insight

  • Promise.race() is about competition.

  • Promise.race() is time-sensitive, not result-sensitive. It returns fastest success OR fastest failure.


Promise.any()

What it does?

  • Waits for the first promise to fulfill (not reject) and its result becomes the outcome. Throws AggregateError if all the promises are rejected.

  • It takes an array (or iterable) of promises and returns a new Promise.

const newPromise = Promise.any(iterableOfPromises);

newPromise
.then()
.catch()
πŸ’‘
Promise.any() = "Give me the first successful result, I don’t care about failures."

Core behavior

1. First successful promise wins

Promise.any([
  Promise.reject("Error 1"),
  Promise.resolve("Success"),
  Promise.resolve("Another success")
])
.then(result => {
  console.log(result);
});

// Output: "Success"

πŸ‘‰ It ignores rejected promises.
πŸ‘‰ Resolves as soon as the first fulfilled promise appears (short-circuiting).

2. Only fails if ALL fail

Promise.any([
  Promise.reject("Error 1"),
  Promise.reject("Error 2")
])
.catch(err => {
  console.log(err.name);    // AggregateError
  console.log(err.errors);  // ["Error 1", "Error 2"]
});

πŸ‘‰ This is the only case where it rejects.
πŸ‘‰ It doesn’t throw a normal error. It throws special error called AggregateError.

Real-world use cases

Best for scenarios where you need the first successful result from a set of tasks, ignoring failures, making it ideal for high-availability systems. Common use cases include:

  1. Trying multiple CDN mirrors.

  2. Querying multiple database read-replicas.

  3. Fetching data from redundant API services.

Important edge cases

1. Empty array

Promise.any([])

πŸ‘‰ Immediately rejects with AggregateError.

2. Non-promises

Promise.any([42, Promise.reject("err")])
  .then(console.log); // Output: 42

πŸ‘‰ Non-promises are treated as already fulfilled.

3. Other promises keep running

Just like race():

πŸ‘‰ No cancellation.
πŸ‘‰ Others continue in background.

πŸ”₯ Interview-level insight

The real power of Promise.any() is:

  • Resilience β†’ system doesn’t break on partial failure

  • Availability β†’ any one service's availability is enough


Comparison

Method Success condition Failure condition
Promise.all() All succeed Any fails
Promise.allSettled() Always succeeds Never fails
Promise.race() First succeeds First fails
Promise.any() First succeeds All fail

Happy learning!