promise-function-async
Require any function or method that returns a Promise to be marked async.
此规则报告的一些问题可通过 --fix
ESLint 命令行选项自动修复。
该规则需要 类型信息 才能运行,但这会带来性能方面的权衡。
确保每个功能只能:
¥Ensures that each function is only capable of:
-
返回被拒绝的 promise,或者
¥returning a rejected promise, or
-
抛出一个 Error 对象。
¥throwing an Error object.
相比之下,非 async
、Promise
返回函数在技术上可以做到这两者。处理这些函数结果的代码通常需要处理这两种情况,这可能会变得复杂。此规则的实践消除了创建代 码来处理这两种情况的要求。
¥In contrast, non-async
, Promise
-returning functions are technically capable of either.
Code that handles the results of those functions will often need to handle both cases, which can get complex.
This rule's practice removes a requirement for creating code to handle both cases.
当函数隐式返回
Promise
和非Promise
类型的联合时,通常是一个错误 - 此规则会标记这些情况。如果是故意的,请显式设置返回类型以允许规则通过。¥When functions return unions of
Promise
and non-Promise
types implicitly, it is usually a mistake—this rule flags those cases. If it is intentional, make the return type explicitly to allow the rule to pass.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/promise-function-async": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/promise-function-async": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples