Skip to content
← Back to rules

unicorn/no-await-in-promise-methods 正确性

This rule is turned on by default.
An auto-fix is available for this rule.

它的作用

禁止在 Promise 方法参数中使用 await

为什么这是个问题?

在传入 Promise.all()Promise.allSettled()Promise.any()Promise.race()Promise 参数上使用 await 很可能是错误的。

示例

以下为 不正确 的代码示例:

javascript
async function foo() {
  Promise.all([await promise, anotherPromise]);
  Promise.allSettled([await promise, anotherPromise]);
  Promise.any([await promise, anotherPromise]);
  Promise.race([await promise, anotherPromise]);
}

以下为 正确 的代码示例:

javascript
async function foo() {
  Promise.all([promise, anotherPromise]);
  Promise.allSettled([promise, anotherPromise]);
  Promise.any([promise, anotherPromise]);
  Promise.race([promise, anotherPromise]);
}

如何使用

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

json
{
  "rules": {
    "unicorn/no-await-in-promise-methods": "error"
  }
}
bash
oxlint --deny unicorn/no-await-in-promise-methods

参考资料