Skip to main content

require-await

Disallow async functions which do not return promises and have no await expression.

💡

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

💭

该规则需要 类型信息 才能运行,但这会带来性能方面的权衡。

🧱

This is an "extension" rule that replaces a core ESLint rule to work with TypeScript. See Rules > Extension Rules.

This rule extends the base require-await rule from ESLint core. 它使用类型信息允许将承诺返回函数标记为 async,而不包含 await 表达式。

¥It uses type information to allow promise-returning functions to be marked as async without containing an await expression.

注意

异步生成器函数 中的 yield 表达式的行为与同步生成器函数不同(它们解开承诺),因此基本规则永远不会检查异步生成器函数。另一方面,我们的规则使用类型信息,可以检测从不使用 await 并且始终产生非承诺值的异步生成器函数。

¥yield expressions in async generator functions behave differently from sync generator functions (they unwrap promises), so the base rule never checks async generator functions. On the other hand, our rule uses type information and can detect async generator functions that both never use await and always yield non-promise values.

示例

¥Examples

async function returnNumber() {
return 1;
}

async function* asyncGenerator() {
yield 1;
}

const num = returnNumber();
const callAsyncGenerator = () => asyncGenerator();
Open in Playground

选项

See eslint/require-await's options.

如何使用

eslint.config.mjs
export default tseslint.config({
rules: {
// Note: you must disable the base rule as it can report incorrect errors
"require-await": "off",
"@typescript-eslint/require-await": "error"
}
});

在线运行试试这个规则 ↗

何时不使用它

Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.

See Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.

'## 资源'

Taken with ❤️ from ESLint core.