await-thenable
Disallow awaiting a value that is not a Thenable.
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.
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.
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.
module.exports = {
"rules": {
"@typescript-eslint/await-thenable": "error"
}
};
Try this rule in the playground ↗
Examples
- ❌ Incorrect
- ✅ Correct
await 'value';
const createValue = () => 'value';
await createValue();
Open in Playgroundawait Promise.resolve('value');
const createValue = async () => 'value';
await createValue();
Open in PlaygroundOptions
This rule is not configurable.
When Not To Use It
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 await
s 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.
Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting. See Performance Troubleshooting if you experience performance degredations after enabling type checked rules.