unicorn/no-single-promise-in-promise-methods
Disallow an array with one item in Promise.all(), Promise.any(), and Promise.race()
fixable
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#
example.js3 problems
async function refresh() {
await Promise.allWrapping single-element array with Promise.all() is unnecessary.([saveDraft()]);
const [user] = await Promise.allWrapping single-element array with Promise.all() is unnecessary.([loadUser()]);
const posts = await Promise.raceWrapping single-element array with Promise.race() is unnecessary.([loadPosts()]);
return { user, posts };
}example.jsNo problems
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:
example.jsNo problems
async function refresh(tasks) {
await Promise.all([loadUser(), loadPosts()]);
await Promise.all([...tasks]);
await Promise.all(tasks);
}