promise/prefer-await-to-then 风格
它的作用
优先使用 await 而不是 then()/catch()/finally() 来读取 Promise 值
为什么这是不好的?
异步/等待语法在可读性上通常更优。
示例
此规则的 错误 代码示例:
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
默认值: false
如果为 true,则即使在 await 或 yield 表达式之后也强制执行该规则。
如何使用
要通过配置文件或 CLI 启用 此规则,可以使用:
json
{
"plugins": ["promise"],
"rules": {
"promise/prefer-await-to-then": "error"
}
}bash
oxlint --deny promise/prefer-await-to-then --promise-plugin