await 默认串行执行,需用 Promise.all() 并发发起请求以缩短总耗时;Promise.allSettled() 可处理部分失败场景;批量请求应控制并发数防压垮服务。

直接用 await 本身不会让请求并行——它天生是串行的。要并行,得绕过 await 的等待机制,改用 Promise.all() 批量触发所有请求,再统一 await 结果。
为什么 await 默认是串行的
await 会暂停当前 async 函数的执行,等前一个 Promise settle 后才继续下一行。比如:
async function fetchSequential() {
const a = await fetch('/api/a'); // 等完才开始 b
const b = await fetch('/api/b'); // 等完才开始 c
const c = await fetch('/api/c');
return [a, b, c];
}
三个请求依次发起,总耗时 ≈ t₁ + t₂ + t₃。
用 Promise.all() 实现并行
关键:先构造所有 Promise(不 await),再用 Promise.all() 并发触发、统一等待:
在 Java 中初始化和管理阿里云 SDK客户端。包括单例模式、线程安全、endpoint 与 region 配置、VPC 终端节点、同步与异步等。
立即学习“Java免费学习笔记(深入)”;
async function fetchParallel() {
// 同时发起三个请求(不 await!)
const promiseA = fetch('/api/a');
const promiseB = fetch('/api/b');
const promiseC = fetch('/api/c');
// 一起等待全部完成
const [a, b, c] = await Promise.all([promiseA, promiseB, promiseC]);
return [a, b, c];
}
- 三个
fetch()立即执行,浏览器底层并发发出网络请求 -
Promise.all()返回一个新 Promise,等所有子 Promise 都 fulfilled 才 resolve - 总耗时 ≈ max(t₁, t₂, t₃),显著缩短等待时间
处理失败情况:allSettled 更稳健
Promise.all() 一有 reject 就整体失败。如果某个接口可能临时不可用,但你想拿到其他成功结果,用 Promise.allSettled():
async function fetchWithFallback() {
const results = await Promise.allSettled([
fetch('/api/a'),
fetch('/api/b'),
fetch('/api/c')
]);
return results.map(r => r.status === 'fulfilled' ? r.value : null);
}
- 每个结果都是 { status: 'fulfilled' | 'rejected', value | reason }
- 即使某请求 404 或超时,也不中断其余请求
- 适合非强依赖型接口组合(如用户信息 + 头像 + 推荐列表)
进阶:动态请求列表 + 控制并发数
当请求太多(比如 100 个 ID 查详情),全量并发可能压垮服务或触发限流。可用 Promise.all() 分批 + 递归/循环控制并发数:
async function fetchInBatches(urls, limit = 5) {
const batches = [];
for (let i = 0; i < urls.length; i += limit) {
batches.push(urls.slice(i, i + limit));
}
const allResults = [];
for (const batch of batches) {
const promises = batch.map(url => fetch(url));
const results = await Promise.all(promises);
allResults.push(...results);
}
return allResults;
}
- 每次只并发发起最多
limit个请求 - 一批完成后再发下一批,平衡速度与稳定性
- 比单个 await 快,又比全量并发更安全

















