Skip to content
← Back to rules

promise/avoid-new Style

它的作用

禁止使用 new Promise() 创建 promise。

为什么这不好?

许多使用 new Promise() 的场景都可以重构为使用 async 函数。现代 JavaScript 中,async 被认为更符合惯用写法。

示例

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

javascript
function foo() {
  return new Promise((resolve, reject) => {
    /* ... */
  });
}

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

javascript
async function foo() {
  // ...
}
const bar = await Promise.all([baz(), bang()]);

如何使用

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

json
{
  "plugins": ["promise"],
  "rules": {
    "promise/avoid-new": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["promise"],
  rules: {
    "promise/avoid-new": "error",
  },
});
bash
oxlint --deny promise/avoid-new --promise-plugin

版本

此规则于 v0.6.1 中添加。

参考资料