核心区别对比表
| 特性 | Promise.all | Promise.allSettled |
|---|
| 成功条件 | 所有 Promise 都成功 | 所有 Promise 都完成(无论成功失败) |
| 失败条件 | 任何一个 Promise 失败就立即失败 | 永远不会失败 |
| 返回值 | 成功值数组 | 状态对象数组 |
| 设计目的 | 需要所有结果都成功才能继续 | 需要知道每个 Promise 的最终状态 |
| ES版本 | ES6 (2015) | ES2020 |
| 使用场景 | 并行依赖的操作 | 独立的并行操作 |
详细对比
1. 行为差异
Promise.all - 全有或全无
const p1 = Promise.resolve('成功1');
const p2 = Promise.reject('错误2');
const p3 = Promise.resolve('成功3');
Promise.all([p1, p2, p3])
.then(results => {
console.log('全部成功:', results);
})
.catch(error => {
console.log('有一个失败:', error);
});
- 只要有一个失败,立即失败
- 其他 Promise 的结果会被丢弃
Promise.allSettled - 全部完成
const p1 = Promise.resolve('成功1');
const p2 = Promise.reject('错误2');
const p3 = Promise.resolve('成功3');
Promise.allSettled([p1, p2, p3])
.then(results => {
console.log('全部完成:');
results.forEach((result, index) => {
if (result.status === 'fulfilled') {
console.log(`p${index + 1}: 成功 - ${result.value}`);
} else {
console.log(`p${index + 1}: 失败 - ${result.reason}`);
}
});
});
- 等待所有 Promise 完成
- 返回每个 Promise 的完整状态信息
2. 返回值结构不同
Promise.all 返回值
Promise.all([Promise.resolve(1), Promise.resolve(2)])
.then(values => console.log(values));
Promise.all([Promise.resolve(1), Promise.reject('错误')])
.catch(error => console.log(error));
Promise.allSettled 返回值
Promise.allSettled([
Promise.resolve(1),
Promise.reject('错误'),
Promise.resolve(3)
])
.then(results => {
console.log(results);
});
3. 实际应用场景
适合 Promise.all 的场景
async function loadDashboard() {
try {
const [user, orders, notifications] = await Promise.all([
fetchUser(),
fetchOrders(),
fetchNotifications()
]);
renderDashboard({ user, orders, notifications });
} catch (error) {
showErrorPage('加载数据失败');
}
}
async function processOrder(orderId) {
const [order, inventory, payment] = await Promise.all([
getOrder(orderId),
checkInventory(orderId),
verifyPayment(orderId)
]);
return { order, inventory, payment };
}
适合 Promise.allSettled 的场景
async function sendNotifications(users) {
const results = await Promise.allSettled(
users.map(user => sendNotification(user))
);
const successful = results
.filter(r => r.status === 'fulfilled')
.map(r => r.value);
const failed = results
.filter(r => r.status === 'rejected')
.map(r => r.reason);
console.log(`发送成功: ${successful.length}, 失败: ${failed.length}`);
return { successful, failed };
}
async function getDataFromMultipleSources() {
const results = await Promise.allSettled([
fetchFromPrimaryAPI().catch(() => null),
fetchFromBackupAPI1().catch(() => null),
fetchFromBackupAPI2().catch(() => null)
]);
for (const result of results) {
if (result.status === 'fulfilled' && result.value) {
return result.value;
}
}
throw new Error('所有数据源都失败了');
}
async function cleanupResources(resources) {
const cleanupResults = await Promise.allSettled(
resources.map(resource => resource.cleanup())
);
logCleanupResults(cleanupResults);
}
4. 错误处理差异
Promise.all([task1(), task2(), task3()])
.then(([result1, result2, result3]) => {
})
.catch(error => {
console.error('某个任务失败:', error);
});
Promise.allSettled([task1(), task2(), task3()])
.then(results => {
const errors = results
.filter(r => r.status === 'rejected')
.map(r => r.reason);
const successes = results
.filter(r => r.status === 'fulfilled')
.map(r => r.value);
if (errors.length > 0) {
console.log(`${errors.length} 个任务失败,但继续处理成功的`);
}
return { successes, errors };
});
5. 互相模拟实现
function promiseAll(promises) {
return Promise.allSettled(promises)
.then(results => {
const rejected = results.find(r => r.status === 'rejected');
if (rejected) {
throw rejected.reason;
}
return results.map(r => r.value);
});
}
function promiseAllSettled(promises) {
const wrappedPromises = promises.map(p =>
Promise.resolve(p).then(
value => ({ status: 'fulfilled', value }),
reason => ({ status: 'rejected', reason })
)
);
return Promise.all(wrappedPromises);
}
总结选择建议
使用 Promise.all 当:
- 所有 Promise 必须都成功才能继续
- 操作有强依赖关系
- 一个失败意味着整个操作失败
- 需要快速失败机制
使用 Promise.allSettled 当:
- 需要知道每个 Promise 的最终状态
- 操作是独立的,一个失败不影响其他
- 需要收集所有结果(成功和失败)
- 实现降级机制或备用方案
- 执行清理或日志记录操作
简单记忆:
- Promise.all = "全部成功才算成功"
- Promise.allSettled = "全部完成就是成功"