gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 16:56:28 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com