Skip to content
← Back to rules

promise/avoid-new 风格

它做了什么

禁止使用 new 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()]);

如何使用

要通过配置文件或 CLI 启用 此规则,可以使用:

json
{
  "plugins": ["promise"],
  "rules": {
    "promise/avoid-new": "error"
  }
}
bash
oxlint --deny promise/avoid-new --promise-plugin

参考资料