【发布时间】:2021-07-14 10:08:31
【问题描述】:
我正在寻找一些 eslint 选项,或者在调用类中的异步方法之前检测缺少 'await' 关键字的其他方法。考虑以下代码:
const externalService = require('./external.service');
class TestClass {
constructor() { }
async method1() {
if (!await externalService.someMethod()) {
await this.method2();
}
}
async method2() {
await externalService.someOtherMethod();
}
module.exports = TestClass;
如果我将method1转换为:不会有警告
async method1() {
if (!await externalService.someMethod()) {
this.method2();
}
}
我尝试在“.eslintrc”文件上做:
"require-await": 1,
"no-return-await": 1,
但没有运气。任何人都知道它是否可能? 非常感谢!
【问题讨论】:
-
在没有
await的情况下调用async函数实际上是完全有效的,你只是得到一个Promise作为回报,所以我怀疑 linter 允许这种规则(这是非常严格的) -
@GuerricP 我编辑了代码,method2会调用外部方法(假设你不知道里面的代码是什么,它是第三方代码)。
-
您或许可以使用规则no-floating-promises。它的行为略有不同,但我认为它会抓住这种情况。
-
@Automatico,谢谢,会检查的。
标签: javascript node.js visual-studio-code eslint eslintrc