async api routes
本地来源:Knowledge/World/项目/Practice/Claude-Skills/vercel-react-best-practices/rules/async-api-routes.md
title: Prevent Waterfall Chains in API Routes impact: CRITICAL impactDescription: 2-10× improvement tags: api-routes, server-actions, waterfalls, parallelization
Prevent Waterfall Chains in API Routes
In API routes and Server Actions, start independent operations immediately, even if you don't await them yet.
Incorrect (config waits for auth, data waits for both):
export async function GET(request: Request) {
const session = await auth()
const config = await fetchConfig()
const data = await fetchData(session.user.id)
return Response.json({ data, config })
}
Correct (auth and config start immediately):
export async function GET(request: Request) {
const sessionPromise = auth()
const configPromise = fetchConfig()
const session = await sessionPromise
const [config, data] = await Promise.all([
configPromise,
fetchData(session.user.id)
])
return Response.json({ data, config })
}
For operations with more complex dependency chains, use better-all to automatically maximize parallelism (see Dependency-Based Parallelization).
本文档为站内渲染。原始文件本地路径:saas/source/knowledge-world/Knowledge-World-项目-Practice-Claude-Skills-vercel-react-best--d5a98d.md(仅本地保留,不入库不部署)