Promise.all
Promise.all
Section titled “Promise.all”Introduction
Section titled “Introduction”Promise.all waits for all promises to fulfill. If any promise rejects, the entire Promise.all rejects immediately.
Basic Usage
Section titled “Basic Usage”const promises = [ fetch('/api/users').then(r => r.json()), fetch('/api/posts').then(r => r.json()), fetch('/api/comments').then(r => r.json())];
try { const [users, posts, comments] = await Promise.all(promises); console.log('All data loaded:', { users, posts, comments });} catch (error) { console.error('One request failed:', error);}Fail-Fast Behavior
Section titled “Fail-Fast Behavior”const p1 = Promise.resolve('OK');const p2 = Promise.reject(new Error('Failed!'));const p3 = Promise.resolve('Also OK'); // Never reached
Promise.all([p1, p2, p3]) .then(results => console.log(results)) .catch(err => console.log(err.message)); // 'Failed!'// Promise.all rejects IMMEDIATELY when p2 rejects// p3 is still pending — its result is lostWhen to Use
Section titled “When to Use”- Loading multiple independent data sets in parallel
- Performing parallel operations where all must succeed
- Waiting for several async setup steps before proceeding
Summary
Section titled “Summary”- Resolves with array of results when all promises fulfill
- Rejects immediately if any promise rejects
- Results maintain the order of the input array
- Fail-fast behavior: one failure cancels the whole operation