unicorn/no-unnecessary-await
Disallow await on a value that cannot be a promise
fixable
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#
example.js3 problems
async function load(ids) {
const users = awaitUnexpected await on a non-Promise value [fetchUser(ids[0]), fetchUser(ids[1])];
const retries = awaitUnexpected await on a non-Promise value 3;
const total = awaitUnexpected await on a non-Promise value (users.length + retries);
return total;
}example.jsNo problems
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;
}