require-await
Disallow async functions which do not return promises and have no
await
expression.
Extending "plugin:@typescript-eslint/recommended-type-checked"
in an ESLint configuration enables this rule.
Some problems reported by this rule are manually fixable by editor suggestions.
This rule requires type information to run.
This rule extends the base eslint/require-await
rule.
It uses type information to allow promise-returning functions to be marked as async
without containing an await
expression.
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
- ❌ Incorrect
- ✅ Correct
async function returnNumber() {
return 1;
}
async function* asyncGenerator() {
yield 1;
}
const num = returnNumber();
const callAsyncGenerator = () => asyncGenerator();
Open in Playgroundfunction returnNumber() {
return 1;
}
function* syncGenerator() {
yield 1;
}
const num = returnNumber();
const callSyncGenerator = () => syncGenerator();
Open in PlaygroundOptions
See eslint/require-await
's options.
How to Use
module.exports = {
"rules": {
// Note: you must disable the base rule as it can report incorrect errors
"require-await": "off",
"@typescript-eslint/require-await": "error"
}
};
Try this rule in the playground ↗
When Not To Use It
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 degredations after enabling type checked rules.