Skip to content
← Back to rules

unicorn/no-thenable 正确性

This rule is turned on by default.

它做了什么

禁止使用 then 属性

为什么这是个问题?

如果一个对象被定义为“可执行的(thenable)”,一旦意外地在 await 表达式中使用,可能会导致问题:

示例

此规则的错误代码示例:

javascript
async function example() {
  const foo = {
    unicorn: 1,
    then() {},
  };

  const { unicorn } = await foo;

  console.log("after"); //<- 此行永远不会执行
}

此规则的正确代码示例:

javascript
async function example() {
  const foo = {
    unicorn: 1,
    bar() {},
  };

  const { unicorn } = await foo;

  console.log("after");
}

如何使用

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

json
{
  "rules": {
    "unicorn/no-thenable": "error"
  }
}
bash
oxlint --deny unicorn/no-thenable

参考资料