Skip to main content

await-thenable

Disallow awaiting a value that is not a Thenable.

💡

此规则报告的一些问题可以通过编辑器 建议 手动修复。

💭

该规则需要 类型信息 才能运行。


"Thenable" 值是具有 then 方法的对象,例如 Promise。 await 关键字通常用于检索调用 Thenable 的 then 方法的结果。

英:A "Thenable" value is an object which has a then method, such as a Promise. The await keyword is generally used to retrieve the result of calling a Thenable's then method.

如果对不是 Thenable 的值使用 await 关键字,则立即直接解析该值。 虽然这样做是有效的 JavaScript,但通常是程序员的错误,例如忘记添加括号来调用返回 Promise 的函数。

英:If the await keyword is used on a value that is not a Thenable, the value is directly resolved immediately. While doing so is valid JavaScript, it is often a programmer error, such as forgetting to add parenthesis to call a function that returns a Promise.

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/await-thenable": "error"
}
};
在线运行试试这个规则 ↗

示例

await 'value';

const createValue = () => 'value';
await createValue();
Open in Playground

何时不使用它

如果你想让代码允许 await 个非 Promise 值。 例如,如果你的框架正在从一种异步代码风格过渡到另一种风格,则不必要地包含 await 可能会很有用。 这通常不是首选,但有时对于视觉一致性很有用。 你可以考虑在这些特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。

英:If you want to allow code to await non-Promise values. For example, if your framework is in transition from one style of asynchronous code to another, it may be useful to include awaits unnecessarily. This is generally not preferred but can sometimes be useful for visual consistency. You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.

选项

该规则不可配置。

资源