Skip to content

unicorn/no-single-promise-in-promise-methods Correctness

This rule is turned on by default.
🛠️ An auto-fix is available for this rule for some violations.

What it does

Disallow passing single-element arrays to Promise methods

Why is this bad?

Passing a single-element array to Promise.all(), Promise.any(), or Promise.race() is likely a mistake.

Example

Examples of incorrect code for this rule:

javascript
async function bad() {
  const foo = await Promise.all([promise]);
  const foo = await Promise.any([promise]);
  const foo = await Promise.race([promise]);
  const promise = Promise.all([nonPromise]);
}

Examples of correct code for this rule:

javascript
async function good() {
  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]);
}

References

Released under the MIT License.