Setting the file. One moment.
Skill 03 · Vercel React Best Practices
Subchapter 3.10
rules/async-api-routes.mdMarkdown1 KBView on GitHub
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).