Lint警告,用于在没有TypeScript的情况下调用.then()或await的异步函数 [英] Lint warning for invoking an async function without `.then()` or `await` without TypeScript
问题描述
在TypeScript中,可以检查并警告开发人员是否正在同步调用 async
函数.
In TypeScript, it is possible to check and warn the developer if they are calling an async
function synchronously.
对于那些需要较少开销并使用node.js v9.0 +的用户,如果我们有类似的东西,是否有可能让短绒棉给我们一个警告?
For those that want less overhead and using node.js v9.0+, is it possible to have any linter give us a warning if we have something like this?
async function foo() {
return;
}
var result = foo(); // warning right here because there is no await
原因是,除非我们显式地将其命名为 fooAsync
,调查实现或假设一切都是异步的,否则函数是否显然未返回诺言/正在等待.也许开发人员搞砸了,却忘了写 await
.
The reason being is that it is not apparently if a function is returning a promise/is await unless we explicitly name it fooAsync
, look into the implementation, or assume everything is async. Or maybe devs messed up and forgot to write await
.
只需要一个警告即可在开发时而不是运行时捕获错误.
Just want a warning to catch errors at dev time and not runtime.
推荐答案
否,这是不可能的.如您所说,它需要类型推断或至少注释来决定一个函数是否将返回一个Promise.这就是像TypeScript编译器那样的类型检查器,而不是linter.
No, that is not possible. As you say, it requires type inference or at least annotations to decide whether a function will return a promise. That's what a typechecker like the TypeScript compiler does, not a linter.
这篇关于Lint警告,用于在没有TypeScript的情况下调用.then()或await的异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!