简体   繁体   English

在没有TypeScript的情况下调用没有`.then()`或`await`的异步函数的Lint警告

[英]Lint warning for invoking an async function without `.then()` or `await` without TypeScript

In TypeScript, it is possible to check and warn the developer if they are calling an async function synchronously. 在TypeScript中,如果开发人员同步调用async函数,则可以检查并警告开发人员。

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? 对于那些需要较少开销并使用node.js v9.0 +的人来说,如果我们有这样的东西,是否可以让任何linter给我们一个警告?

async function foo() {
  return;
}

var result = foo(); // warning right here because there is no 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. 原因是,除非我们明确地将其命名为fooAsync ,查看实现或假设所有内容都是异步的,否则显然不是函数返回promise / is await。 Or maybe devs messed up and forgot to write await . 或者也许开发人员搞砸了,忘了写await

Just want a warning to catch errors at dev time and not runtime. 只需要一个警告来捕获开发时的错误,而不是运行时。

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. 如你所说,它需要类型推断或至少注释来决定函数是否会返回一个promise。 That's what a typechecker like the TypeScript compiler does, not a linter. 这就像TypeScript编译器那样的类型检查器,而不是linter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM