针对调用不带“.then()”或不带 TypeScript 的“await”的异步函数的 Lint 警告

PGT*_*PGT 6 javascript node.js async-await eslint

在 TypeScript 中,可以检查并警告开发人员是否同步调用函数async

对于那些想要减少开销并使用 Node.js v9.0+ 的人来说,如果我们有这样的东西,是否可以让任何 linter 给我们一个警告?

async function foo() {
  return;
}

var result = foo(); // warning right here because there is no await
Run Code Online (Sandbox Code Playgroud)

原因是,除非我们显式命名它fooAsync、查看实现或假设一切都是异步的,否则函数是否返回承诺/是否等待并不明显。或者也许开发人员搞砸了并且忘记写了await

只是想要一个警告来捕获开发时而不是运行时的错误。

Ber*_*rgi 4

不,那是不可能的。正如您所说,它需要类型推断或至少注释来决定函数是否返回承诺。这就是像 TypeScript 编译器这样的类型检查器所做的事情,而不是 linter。

  • @slhck typescript-eslint 确实有 [*no-floating-promises*](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-floating -promises.md) 和 [*await-thenable*](https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/await-thenable.md)规则 (4认同)