Linter · Rules
unicorn/no-unnecessary-await
Disallow await on a value that cannot be a promise
await on something that isn't a promise gives you back the same value one tick later. It usually means the await is in the wrong place, like on an array of promises instead of Promise.all(), or on a function instead of its call.
The rule doesn't use types. It reports literals, arrays, template strings, functions, classes, JSX, another await, and the results of operators such as +, ! and typeof. A variable, a call or an object literal could be a promise, so those aren't reported.
--fix removes the await where that's safe.
Configure
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"unicorn/no-unnecessary-await" = "error" # or "warn"
Examples
Reported:
async function load(ids) {
const users = await [fetchUser(ids[0]), fetchUser(ids[1])];
const retries = await 3;
const total = await (users.length + retries);
return total;
}
Not reported:
async function load(ids) {
const users = await Promise.all([fetchUser(ids[0]), fetchUser(ids[1])]);
const retries = 3;
const settings = await (cached || fetchSettings());
return users.length + retries + settings.extra;
}