noSinglePromiseInPromiseMethods (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.5 - Diagnostic Category:
lint/nursery/noSinglePromiseInPromiseMethods - This rule has an unsafe fix.
- The default severity of this rule is warning.
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noSinglePromiseInPromiseMethods": "error" } } }}Description
Section titled “Description”Disallow passing an array with a single element to Promise.all(), Promise.any(), or Promise.race().
Promise.all(), Promise.any(), and Promise.race() are meant to combine several promises into one.
When the array contains only one element, there is nothing to combine.
The array only makes the code harder to read, and it often means that another promise is missing from it.
If the call is awaited, await the value directly.
Otherwise, if you need a promise, pass the value to Promise.resolve().
Promise.all() is an exception, because it returns an array that holds the value.
Await the value directly instead, and update the code that reads the array.
The fix for Promise.any() changes the error that is thrown when the promise is rejected.
Promise.any() rejects with an AggregateError that holds the original error in its errors property,
while awaiting the value directly throws the original error.
Check that no code depends on AggregateError before you apply the fix.
Promise.allSettled() is not reported, because its result has a different shape than the promise it receives.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const foo = await Promise.all([promise]);code-block.js:1:31 lint/nursery/noSinglePromiseInPromiseMethods ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This array passed to Promise.all() has only one element.
> 1 │ const foo = await Promise.all([promise]);
│ ^^^^^^^^^
2 │
ℹ Promise.all() is meant to combine several promises. With only one element, the array is unnecessary, or another promise is missing from it.
ℹ If the array is complete, await the value directly, and update the code that reads the array that Promise.all() returns.
ℹ 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.
const foo = await Promise.any([promise]);code-block.js:1:31 lint/nursery/noSinglePromiseInPromiseMethods FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This array passed to Promise.any() has only one element.
> 1 │ const foo = await Promise.any([promise]);
│ ^^^^^^^^^
2 │
ℹ Promise.any() is meant to combine several promises. With only one element, the array is unnecessary, or another promise is missing from it.
ℹ If the array is complete, use the value directly, or pass it to Promise.resolve() if you need a promise.
ℹ 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: Await the value directly.
1 │ const·foo·=·await·Promise.any([promise]);
│ ------------- --
const foo = await Promise.race([promise]);code-block.js:1:32 lint/nursery/noSinglePromiseInPromiseMethods FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This array passed to Promise.race() has only one element.
> 1 │ const foo = await Promise.race([promise]);
│ ^^^^^^^^^
2 │
ℹ Promise.race() is meant to combine several promises. With only one element, the array is unnecessary, or another promise is missing from it.
ℹ If the array is complete, use the value directly, or pass it to Promise.resolve() if you need a promise.
ℹ 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: Await the value directly.
1 │ const·foo·=·await·Promise.race([promise]);
│ -------------- --
const promise = Promise.race([nonPromise]);code-block.js:1:30 lint/nursery/noSinglePromiseInPromiseMethods FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This array passed to Promise.race() has only one element.
> 1 │ const promise = Promise.race([nonPromise]);
│ ^^^^^^^^^^^^
2 │
ℹ Promise.race() is meant to combine several promises. With only one element, the array is unnecessary, or another promise is missing from it.
ℹ If the array is complete, use the value directly, or pass it to Promise.resolve() if you need a promise.
ℹ 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: Use Promise.resolve() instead.
1 │ - const·promise·=·Promise.race([nonPromise]);
1 │ + const·promise·=·Promise.resolve(nonPromise);
2 2 │
const foo = await promise;const promise = Promise.resolve(nonPromise);const foo = await Promise.all(promises);const foo = await Promise.any([promise, anotherPromise]);const [{ value: foo, reason: error }] = await Promise.allSettled([promise]);Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.