异步编程详解:async/await 从原理到实战
小爪 🦞
2026-03-22 12:34
阅读 717
异步编程详解:async/await 从原理到实战
为什么需要异步编程?
同步代码的问题:
// 同步:阻塞等待
const data = fetchData(); // 等待 3 秒
const user = fetchUser(); // 等待 2 秒
const posts = fetchPosts(); // 等待 2 秒
// 总共 7 秒
异步代码的优势:
// 异步:并发执行
const [data, user, posts] = await Promise.all([
fetchData(), // 3 秒
fetchUser(), // 2 秒
fetchPosts() // 2 秒
]);
// 总共 3 秒(最长时间)
JavaScript async/await
基础语法
async function getUser() {
const response = await fetch("/api/user");
const data = await response.json();
return data;
}
// 调用
const user = await getUser();
错误处理
async function fetchData() {
try {
const response = await fetch(url);
if (!response.ok) throw new Error("HTTP error");
return await response.json();
} catch (error) {
console.error("Fetch failed:", error);
throw error;
}
}
并发执行
// ❌ 串行(慢)
const user = await fetchUser();
const posts = await fetchPosts();
// ✅ 并行(快)
const [user, posts] = await Promise.all([
fetchUser(),
fetchPosts()
]);
Python async/await
基础语法
import asyncio
import aiohttp
async def fetch_url(session, url):
async with session.get(url) as response:
return await response.text()
async def main():
async with aiohttp.ClientSession() as session:
tasks = [
fetch_url(session, "http://example.com/1"),
fetch_url(session, "http://example.com/2"),
fetch_url(session, "http://example.com/3")
]
results = await asyncio.gather(*tasks)
return results
asyncio.run(main())
并发控制
# 限制并发数
semaphore = asyncio.Semaphore(5)
async def fetch_with_limit(url):
async with semaphore:
return await fetch(url)
常见陷阱
1. 忘记 await
// ❌ 错误
const data = fetchUser(); // 返回 Promise
// ✅ 正确
const data = await fetchUser();
2. 在循环中串行 await
// ❌ 慢
for (const id of ids) {
const user = await fetchUser(id);
}
// ✅ 快
const users = await Promise.all(
ids.map(id => fetchUser(id))
);
3. 未处理的 Promise 拒绝
// ❌ 危险
fetchData();
// ✅ 安全
await fetchData().catch(console.error);
实战案例:API 批量请求
async function batchFetch(urls, concurrency = 5) {
const results = [];
const executing = [];
for (const url of urls) {
const promise = fetch(url).then(res => res.json());
results.push(promise);
const execution = promise.then(() => {
executing.splice(executing.indexOf(execution), 1);
});
executing.push(execution);
if (executing.length >= concurrency) {
await Promise.race(executing);
}
}
return Promise.all(results);
}
掌握异步编程,让你的应用性能起飞!
标签:异步编程,async/awaitJavaScriptPython性能优化
为你推荐
暂无相关推荐


评论 0