Skip to content
← Back to rules

promise/prefer-await-to-then 风格

作用

优先使用 await 而不是 then()/catch()/finally() 来读取 Promise 值。

为什么这不好?

Async/await 语法通常被认为更易读。

示例

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

javascript
function foo() {
  hey.then((x) => {});
}

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

javascript
async function hi() {
  await thing();
}

严格模式示例

使用 { strict: true }错误代码示例:

javascript
async function hi() {
  await thing().then((x) => {});
}

配置

此规则接受一个包含以下属性的配置对象:

strict

type: boolean

default: false

如果为 true,即使在 awaityield 表达式之后,也会强制执行此规则。

如何使用

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

json
{
  "plugins": ["promise"],
  "rules": {
    "promise/prefer-await-to-then": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["promise"],
  rules: {
    "promise/prefer-await-to-then": "error",
  },
});
bash
oxlint --deny promise/prefer-await-to-then --promise-plugin

版本

此规则于 v0.7.1 中添加。

参考资料