Linter · Rules
@typescript-eslint/await-thenable
Disallow await on a value that is not a promise
await on a value that has no then method gives back the same value one tick later. It's usually a mistake. Either the function was never async, or you forgot to call it.
The same goes for for await...of over something that isn't async iterable, await using with something that isn't async disposable, and values that aren't promises in the array passed to Promise.all, Promise.allSettled, Promise.any, or Promise.race.
bun lint --type-aware turns this rule on in TypeScript files.
Configure
Off by default. This rule needs types, so turning it on makes bun lint type check your code first.
bunfig.toml
[lint.rules]
"@typescript-eslint/await-thenable" = "error" # or "warn"
Examples
Reported:
function readConfig(): string {
return "{}";
}
const config = await readConfig();
const ports = [3000, 3001];
for await (const port of ports) {
console.log(port);
}
async function load(id: number): Promise<string> {
return String(id);
}
await Promise.all([load(1), 2]);
Not reported:
async function readConfig(): Promise<string> {
return "{}";
}
const config = await readConfig();
const ports = [3000, 3001];
for (const port of ports) {
console.log(port);
}
You can await a value that may or may not be a promise, and you can await any and unknown:
async function resolve(value: string | Promise<string>, other: unknown) {
return [await value, await other];
}