推荐的 async 简化可能改变异常与 Promise 行为
原文依据:3 处示例把 async 函数改成普通函数并直接返回底层调用,但该 Skill 同时要求错误行为完全相同。如果 findById 在返回 Promise 前同步抛错,原函数会返回被拒绝的 Promise,而改写后的函数会同步抛出异常;返回 Promise 的对象身份也可能不同。仅凭声明的 Promise 返回类型不能排除这些差异。
调用方的 try/catch、Promise 链、重试或错误上报可能走不同路径,导致未处理异常、跳过恢复逻辑或运行时中断。
该示例是明确推荐的改写,却可能违反 Skill 自己要求的“错误行为完全相同”。如果 `findById` 在返回 Promise 前同步抛错,原 `async` 包装会产生 rejected Promise,改写后则会同步抛错;调用方的捕获方式可能因此失效。用户可要求仅在确认底层调用不会同步抛错且测试覆盖该差异时采用此改写。
### 1. Preserve Behavior ExactlyDon't change what the code does — only how it expresses it. All inputs, outputs, side effects, error behavior, and edge cases must remain identical. If you're not sure a simplification preserves behavior, don't make it.查看另外 2 个位置
```typescript// SIMPLIFY: Unnecessary async wrapper// Beforeasync function getUser(id: string): Promise<User> { return await userService.findById(id);}// Afterfunction getUser(id: string): Promise<User> { return userService.findById(id);}Don't change what the code does — only how it expresses it. All inputs, outputs, side effects, error behavior, and edge cases must remain identical. If you're not sure a simplification preserves behavior, don't make it.