Back to Eliminating Waterfalls

Prevent Waterfall Chains in API Routes

api-routesserver-actionswaterfallsparallelization
28.0k🕒 2026-06-10Source ↗

Install this skill

npx skills add vercel-labs/agent-skills

Works across Claude Code, Cursor, Codex, Copilot & Antigravity

API routes frequently suffer from latency bottlenecks when asynchronous operations are executed sequentially. By default, developers often await functions one after another, forcing the server to wait for the completion of an identity check before initiating database queries or external configuration fetches. This approach wastes idle CPU cycles and inflates response times. Preventing waterfall chains involves initiating independent asynchronous tasks as promises immediately, effectively allowing them to overlap in their execution time. By triggering multiple requests simultaneously rather than blocking on each individual call, you reduce total latency to the duration of the longest request rather than the sum of all requests. This technique specifically targets Next.js API routes and Server Actions, optimizing data fetching patterns and ensuring that resource-heavy operations run concurrently whenever dependencies allow.

When to Use This Skill

  • Fetching user session data and global app configurations simultaneously
  • Executing concurrent database queries that do not depend on each other
  • Optimizing server actions that call multiple microservices or external APIs
  • Managing independent side effects during API route processing

How to Invoke This Skill

Example prompts that trigger this skill in Claude Code, Cursor, or Antigravity:

  • Optimize my API route latency
  • Stop sequential database queries in my Server Action
  • Fix waterfall requests in Next.js
  • How do I run fetch calls in parallel?
  • Reduce total response time for this endpoint

What this skill does

  • Initiates multiple asynchronous requests concurrently without waiting
  • Uses Promise.all for batching independent operations
  • Improves server-side latency in Next.js endpoints
  • Identifies overlapping dependencies versus blocking chains
  • Reduces total request execution time by overlapping I/O wait times

When not to use it

  • When a second operation requires a value returned from the first
  • When hitting rate-limited APIs where concurrent requests trigger blocks
  • When operations involve shared mutable state that requires strict ordering

Example workflow

  1. Identify blocked calls in your API route or Server Action
  2. Assign asynchronous functions to variables without the await keyword
  3. Await the primary dependency if one exists
  4. Group subsequent independent promises into a single Promise.all call
  5. Return the combined data result to the client

Prerequisites

  • Basic understanding of JavaScript Promises
  • Experience with Next.js API Routes or Server Actions

Pitfalls & limitations

  • !Over-parallelizing operations that depend on sequential database states
  • !Running into third-party API rate limits by sending too many requests at once
  • !Difficulty debugging stack traces when multiple promises fail simultaneously

FAQ

Why is my API route still slow after parallelizing?
You might still have a hidden dependency chain where one operation requires the output of another. Ensure only truly independent tasks are grouped in Promise.all.
Does this work for database writes?
It depends on your database. If the writes are independent, it works; however, if they modify the same row or rely on specific insertion orders, keep them sequential.
How does this compare to simply writing more clean code?
It is about architectural performance. Clean code improves readability, but preventing waterfalls directly reduces the time the user spends waiting for a server response.

How it compares

While manual implementation requires careful promise management, this pattern provides a structured, predictable way to avoid block-wait-block loops, whereas generic prompts often fail to account for specific dependency ordering.

Source & trust

28k stars🕒 Updated 2026-06-10
📄 Full skill instructions — original source: vercel-labs/agent-skills
## 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).

How to Use This Skill Unit

Option A: Project-Specific (Recommended)

  1. Click "Download" above
  2. In your project, create the directory: .agent/skills/async-api-routes/
  3. Save the file as SKILL.md
  4. The agent will automatically discover the skill based on its description.

Option B: Global Installation (All Agents)

Save the file to these locations to make it available across all projects:

  • Claude Code: ~/.claude/skills/vercel-labs/agent-skills/async-api-routes/SKILL.md
  • Cursor: ~/.cursor/skills/vercel-labs/agent-skills/async-api-routes/SKILL.md
  • Antigravity: ~/.gemini/antigravity/skills/vercel-labs/agent-skills/async-api-routes/SKILL.md

🚀 Install with CLI:
npx skills add vercel-labs/agent-skills

Read the Master Guide: Mastering Agent Skills

Recommended Rules

View more rules

Recommended Workflows

View more workflows

Recommended MCP Servers

View more MCP servers

Take It Further

Maximize your productivity with these powerful resources

📋

Define Your Standards

Set up coding standards to ensure this workflow produces consistent, high-quality results.

Browse Rules Library
📖

Master Workflows

Learn how to create custom workflows, use Turbo Mode, and build your automation library.

Complete Guide

How to use this Skill in Claude Code & Cursor

For Claude Code (CLI)

To use this skill in Claude Code, copy the rule content into your project's custom instructions or follow our Add-Skill CLI guide. This ensures Claude follows your standards during every code generation.

For Cursor & Windsurf

For Cursor or Windsurf, individual skills are best used in the "Rules for AI" section. This specific unit helps the agent avoid eliminating waterfalls issues, leading to cleaner, more efficient code.

Why the skill format matters: the standardized Agent Skills format lets your AI agent load detailed instructions only when they are relevant, keeping your prompt clean while improving results.

Source & attribution

This skill is categorized under Eliminating Waterfalls and is published by Vercel Engineering, maintained in vercel-labs/agent-skills.

← Browse All Agent Skills
Sponsored AI assistant. Recommendations may be paid.