Linter · Rules
unicorn/no-single-promise-in-promise-methods
Disallow an array with one item in Promise.all(), Promise.any(), and Promise.race()
Promise.all(), Promise.any(), and Promise.race() combine several promises. With one promise there's nothing to combine, so you can await it directly. It's often what's left after someone removed the other items from the array.
--fix unwraps the promise in simple cases, such as the statement await Promise.all([promise]);. It leaves a call alone when its result is stored in a variable, returned, or passed to a function.
Configure
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"unicorn/no-single-promise-in-promise-methods" = "error" # or "warn"
Examples
Reported:
async function refresh() {
await Promise.all([saveDraft()]);
const [user] = await Promise.all([loadUser()]);
const posts = await Promise.race([loadPosts()]);
return { user, posts };
}
Not reported:
async function refresh() {
await saveDraft();
const user = await loadUser();
const posts = await loadPosts();
return { user, posts };
}
An array with several items, a spread, or a variable is fine:
async function refresh(tasks) {
await Promise.all([loadUser(), loadPosts()]);
await Promise.all([...tasks]);
await Promise.all(tasks);
}