noUselessAwait (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.5 - Diagnostic Category:
lint/nursery/noUselessAwait - This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
- Same as
unicorn/no-unnecessary-await
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noUselessAwait": "error" } } }}Description
Section titled “Description”Disallow await on values that are never promises.
The await keyword waits for a promise to settle and returns its result.
Values such as numbers, strings, arrays, functions, and the results of
comparisons are never promises, so awaiting them returns the same value.
The await still pauses the function until other pending work runs,
which makes the code slower and suggests that the value is a promise when it isn’t.
Awaiting an array doesn’t wait for the promises inside it.
Use Promise.all() or Promise.allSettled() to wait for all of them.
The rule only reports values that can be identified as non-promises from the code alone, such as literals, arrays, functions, classes, JSX, and the results of arithmetic, comparison, and unary operators.
The fix is unsafe because removing await makes the code that follows it run earlier.
Biome offers the fix only when the await is the last thing its function or module runs.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”await 1;code-block.js:1:1 lint/nursery/noUselessAwait FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This value is never a promise, so await is unnecessary.
> 1 │ await 1;
│ ^^^^^
2 │
ℹ Awaiting a value that isn't a promise returns the same value, but still delays the code that follows it.
ℹ Remove await. To wait for several promises, pass them to Promise.all() instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Remove await.
1 │ await·1;
│ ------
await [fetchUser(), fetchPosts()];code-block.js:1:1 lint/nursery/noUselessAwait FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This value is never a promise, so await is unnecessary.
> 1 │ await [fetchUser(), fetchPosts()];
│ ^^^^^
2 │
ℹ Awaiting a value that isn't a promise returns the same value, but still delays the code that follows it.
ℹ Remove await. To wait for several promises, pass them to Promise.all() instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Remove await.
1 │ await·[fetchUser(),·fetchPosts()];
│ ------
async function check(a, b) { return await (a === b);}code-block.js:2:12 lint/nursery/noUselessAwait FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This value is never a promise, so await is unnecessary.
1 │ async function check(a, b) {
> 2 │ return await (a === b);
│ ^^^^^
3 │ }
4 │
ℹ Awaiting a value that isn't a promise returns the same value, but still delays the code that follows it.
ℹ Remove await. To wait for several promises, pass them to Promise.all() instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Remove await.
2 │ ····return·await·(a·===·b);
│ ------
await fetchUser();await Promise.all([fetchUser(), fetchPosts()]);await (condition ? fetchUser() : fetchPosts());await new Promise((resolve) => setTimeout(resolve, 100));See Also
Section titled “See Also”- If you want to report
awaiton any value that isn’t a promise, using type information, seeuseAwaitThenable.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.