async-defer-await — Claude Code rule: defer await operations
async-defer-await — a rule that moves await operations into code branches where they're actually needed, preventing blockage of execution paths that may exit early.
MIT ★ 7,191 updated 2026-10-24
.claude/rules/async-defer-await.mdWhat async-defer-await does
When await sits at the start of a function, it blocks all paths until the result arrives—even if a branch returns before using it. This rule suggests moving await inside conditional branches so it triggers only when required.
Examples cover two patterns: a function with skipProcessing that exits without consuming data, and resource handling that checks existence before fetching permissions. In both, reordering operations provides a win—the code doesn't wait if the result won't be used.
The optimization matters when early exits fire often or when the deferred operation is costly (network call, database query). The rule integrates into Claude Code through .claude/rules/async-defer-await.md.
What it helps with
- Avoid blocking code paths that may return before await completes
- Reorder validation checks and conditions before async calls
- Lower response time in functions with early returns
- Defer expensive async operations until they're needed
Example prompts for the agent
- Review handleRequest: the await blocks both branches, move it into the processing path only
- Examine updateResource: fetch the resource first, defer permission checks to avoid blocking 404 cases
- Identify all awaits that precede early returns and relocate them into their actual usage branches
How to install async-defer-await
Manually: put the files in place
.claude/rules/async-defer-await.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/async-defer-await.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/async-defer-await.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/async-defer-await.mdManually: put the files in place
.clinerules/async-defer-await.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about async-defer-await
How do I install async-defer-await in Claude Code?
Create .claude/rules/async-defer-await.md in your project root with the rule content. Claude Code picks it up automatically on the next session.
What does this rule do?
It shows how to move await from the function top into branches where the result is actually consumed. Code runs faster because it skips waiting for unneeded operations.
When does this optimization matter?
When early exits happen frequently or when the deferred operation is expensive (HTTP request, database lookup). If all branches need the data, an early await is fine.
Source text
async-defer-await.md · MIT
---
title: Defer Await Until Needed
impact: HIGH
impactDescription: avoids blocking unused code paths
tags: async, await, conditional, optimization
---
## Defer Await Until Needed
Move `await` operations into the branches where they're actually used to avoid blocking code paths that don't need them.
**Incorrect (blocks both branches):**
```typescript
async function handleRequest(userId: string, skipProcessing: boolean) {
const userData = await fetchUserData(userId)
if (skipProcessing) {
// Returns immediately but still waited for userData
return { skipped: true }
}
// Only this branch uses userData
return processUserData(userData)
}
```
**Correct (only blocks when needed):**
```typescript
async function handleRequest(userId: string, skipProcessing: boolean) {
if (skipProcessing) {
// Returns immediately without waiting
return { skipped: true }
}
// Fetch only when needed
const userData = await fetchUserData(userId)
return processUserData(userData)
}
```
**Another example (early return optimization):**
```typescript
// Incorrect: always fetches permissions
async function updateResource(resourceId: string, userId: string) {
const permissions = await fetchPermissions(userId)
const resource = await getResource(resourceId)
if (!resource) {
return { error: 'Not found' }
}
if (!permissions.canEdit) {
return { error: 'Forbidden' }
}
return await updateResourceData(resource, permissions)
}
// Correct: fetches only when needed
async function updateResource(resourceId: string, userId: string) {
const resource = await getResource(resourceId)
if (!resource) {
return { error: 'Not found' }
}
const permissions = await fetchPermissions(userId)
if (!permissions.canEdit) {
return { error: 'Forbidden' }
}
return await updateResourceData(resource, permissions)
}
```
This optimization is especially valuable when the skipped branch is frequently taken, or when the deferred operation is expensive.
© tech-leads-club · MIT · tech-leads-club/agent-skills