检测VSCode中JavaScript方法中缺少的wait
我正在搜索一些eslint选项,或者在类内调用异步方法之前检测缺少'await'关键字的其他方法。考虑下面的代码:检测VSCode中JavaScript方法中缺少的wait,javascript,node.js,visual-studio-code,eslint,eslintrc,Javascript,Node.js,Visual Studio Code,Eslint,Eslintrc,我正在搜索一些eslint选项,或者在类内调用异步方法之前检测缺少'await'关键字的其他方法。考虑下面的代码: const externalService = require('./external.service'); class TestClass { constructor() { } async method1() { if (!await externalService.someMethod()) { await this.method2();
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,
但是没有运气。有人知道这是否可能吗?
非常感谢
require await
表示“不要使函数async
,除非在函数内部使用await
”
这是因为async
有两种效果:
- 它强制函数返回一个承诺
- 它允许您在其中使用
wait
wait
,则需要询问为什么将其标记为async
no return wait
阻止您执行以下操作:
return await something
因为await
从承诺中解包一个值,但从async
函数返回一个值将其包装在承诺中
因为仅仅返回一个承诺就导致该承诺被采纳,所以将return
与await
结合起来就太夸张了
所以这两个都不是你想要的 这就引出了你的真实愿望 据我所知,这样的功能在ESLint中并不存在,我认为拥有这样的功能是没有用的。 在许多用例中,您不想等待
异步
函数返回的内容
e、 g
上面是一个常见的用例,您希望并行运行一组异步函数,然后等待它们全部解析
另一个例子是no return wait
设计用于检测
像这样的情况很常见,大多数人都不希望他们的工具链让他们这么做。在没有
await
的情况下调用async
函数实际上是完全有效的,你只会得到一个Promise
作为回报,所以我怀疑Linter是否允许这种规则(这是非常严格的)@如果我编辑了代码,method2将调用外部方法(假设您不知道其中的代码是什么,它是第三方代码)。您可以使用规则。它的行为稍有不同,但我认为它会抓住这个案子。@Automatico,谢谢,会检查它的。谢谢你的回答!但我希望Eslint能让我选择控制它,如果我想在这种情况下发出警告或不发出警告的话。昆汀,谢谢,很高兴知道:)
const array_of_promises = array_of_values.map( value => do_something_async(value) );
const array_of_resolved_values = await Promise.all(array_of_promises);