javascript - 检测 Javascript/ECMAScript 等待滥用?

标签 javascript async-await eslint tslint

Javascript 或 Typescript 生态系统中是否有工具可以检测同步函数上 await 的(错误/过度)使用? (例如 tslint 规则)

当我错误地使用await作为同步函数的结果时,问题就出现了,

const decoded = await jsonwebtoken.verify(session, publicKey, opts);

我的 linter(默认的 gts tslint)没有捕获到。该代码奇怪地工作(await 默默地传递非 promise ?),但我想标记滥用以鼓励使用异步/回调选项。

我原以为这种对 await 的滥用会触发 Typescript 错误,但 tscts-jest 通过了以下测试:

test('Demonstrate await', async () => {
  function foo(): number { return 4; }
  const food = await foo(); // Even replace foo() with a constant or literal.
  expect(food).toBe(4);
});

最佳答案

是的,如果您使用类型系统,则可以强制执行类似的 linter 规则。对于 TSlint,你可以使用这样的东西:

https://palantir.github.io/tslint/rules/await-promise/

关于javascript - 检测 Javascript/ECMAScript 等待滥用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59021884/

相关文章:

javascript - 如果所有迭代都返回空,则执行某些操作,如果其中 1 次迭代返回某些内容,则执行其他操作

c# - 获取取消 token 或公开取消方法?

eslint - 如何禁用 esLint 上的缩进检查?

javascript - Vuejs : V-HTML data binding of html data against eslint rule

typescript - 如何修复 'Definition for rule ' @typescript-eslint/no-use-before-declare' is not found.eslint(@typescript-eslint/no-use-before-declare)'

javascript - Google map 使用 php 和 jquery 动态显示许多标记

javascript - Angular v4 : Do we store data in a Service or the Component or both?

javascript - Kendo Angular 自动完成在结果中显示对象

Flutter 如何处理异步/等待中的错误

node.js - 通过 Node 请求包递归使用 async/await