Promise.all()

ES6+

Returns a single Promise that resolves when all of the promises in the iterable have resolved, or rejects when any promise rejects.

Syntax

Promise.all(iterable)

Parameters

iterable Iterable

An iterable of promises

Return Value

Promise

A Promise that resolves with an array of all the resolved values

Examples

JavaScript
const p1 = Promise.resolve(1);
const p2 = Promise.resolve(2);
const p3 = Promise.resolve(3);

Promise.all([p1, p2, p3])
  .then(values => console.log(values));
Output:
// [1, 2, 3]

📌 When to Use

Use Promise.all() when you need all async operations to succeed, like loading multiple resources simultaneously where any failure should stop the process.

⚠️ Common Mistakes

Using Promise.all() when partial success is acceptable - use Promise.allSettled() instead.

Passing an empty array returns immediately with [], which might cause unexpected behavior.

✅ Best Practices

Use destructuring to get individual results: const [user, posts] = await Promise.all([fetchUser(), fetchPosts()]).

Limit concurrent requests to avoid overwhelming APIs: batch promises into smaller groups.

⚡ Performance Notes

Promise.all() runs all promises in parallel, making it much faster than sequential await for independent operations. The total time equals the slowest promise.

🌍 Real World Example

Dashboard Data Loader

Load multiple data sources in parallel for a dashboard, requiring all to succeed.

async function loadDashboard(userId) {
  try {
    const [user, stats, notifications, settings] = await Promise.all([
      fetch(`/api/users/${userId}`).then(r => r.json()),
      fetch(`/api/users/${userId}/stats`).then(r => r.json()),
      fetch(`/api/users/${userId}/notifications`).then(r => r.json()),
      fetch(`/api/users/${userId}/settings`).then(r => r.json())
    ]);

    return {
      success: true,
      data: { user, stats, notifications, settings }
    };
  } catch (error) {
    return {
      success: false,
      error: 'Failed to load dashboard data'
    };
  }
}

// All 4 requests run in parallel, total time = slowest request

Related Methods