漏了一个 await
Dropped an await
重构之后,一个异步调用返回的 promise 被丢弃了,导致写入顺序错乱,或者错误变成了无人处理的 rejection。
适用于: Claude Code · Cursor · Codex · Copilot agent
症状
偶发的“保存了但没显示”问题、不稳定的测试、日志里的 unhandled rejection 警告。
为什么会这样
函数看起来像同步的时候,智能体就容易漏掉 await,特别是刚把它改成 async 之后。agent-review 列为 AR022(高严重度)。
示例
摘自 agent-review 分类(AR022)。
tsasync function persist() {
save(record); // returns Promise<void>; not awaited
}
复制即用的修复
按你用的智能体挑选文件。规则片段用英文写,放进任何仓库都能用。
规则
Claude Code:加入 CLAUDE.md
CLAUDE.md## Async discipline
- When you make a function async (or call one), update every call site to `await` it or handle the
promise explicitly. List the call sites you changed.
- Never leave a floating promise. If fire-and-forget is intended, write `void task().catch(report)` with a comment.
- Keep `@typescript-eslint/no-floating-promises` on; don't disable it to get green.
规则
Codex、Copilot 编码智能体等:加入 AGENTS.md
AGENTS.md## Async discipline
- When you make a function async (or call one), update every call site to `await` it or handle the
promise explicitly. List the call sites you changed.
- Never leave a floating promise. If fire-and-forget is intended, write `void task().catch(report)` with a comment.
- Keep `@typescript-eslint/no-floating-promises` on; don't disable it to get green.
Cursor 规则
Cursor:项目规则
.cursor/rules/unawaited-promise.mdc---
description: "AgentFails: Dropped an await"
globs: **/*.{ts,tsx,js,jsx}
alwaysApply: false
---
## Async discipline
- When you make a function async (or call one), update every call site to `await` it or handle the
promise explicitly. List the call sites you changed.
- Never leave a floating promise. If fire-and-forget is intended, write `void task().catch(report)` with a comment.
- Keep `@typescript-eslint/no-floating-promises` on; don't disable it to get green.
CI / 脚本
ESLint:把悬空 promise 设为错误
eslint.config.mjs// requires typed linting (typescript-eslint with parserOptions.projectService: true)
rules: {
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-misused-promises": "error",
}