Skip to content
← Back to rules

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.

作用

禁止将单元素数组传递给 Promise 方法。

为什么这不好?

将单元素数组传递给 Promise.all()Promise.any()Promise.race() 很可能是一个错误。

示例

以下是此规则的错误代码示例:

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]);
}

以下是此规则的正确代码示例:

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]);
}

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/no-single-promise-in-promise-methods": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/no-single-promise-in-promise-methods": "error",
  },
});
bash
oxlint --deny unicorn/no-single-promise-in-promise-methods

版本

此规则在 v0.2.18 中添加。

参考资料