在JavaScript中产生新型错误 [英] Making new type of errors in JavaScript

查看:243
本文介绍了在JavaScript中产生新型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶尔会忘记使用await关键字,例如:

I occasionally forget to use the await keyword, e.g.:

const foo = bar();
// instead of
const foo = await bar();

这些错误很难发现.可以使用哪些方法,库或工具来防止此类错误?

Those bugs are difficult to spot. Which methods, libraries or tools are available to prevent those kind of bugs?

推荐答案

您应该使用诸如 ESLint 之类的短绒.如果将该工具与IDE/Editor集成在一起,并且启用了默认规则集(或您自己的规则集),它将使您的工作变得更加轻松.

You should use a linter like ESLint. If you integrate the tool with your IDE/Editor and you enable the default set of rules (or your own) it'll make your life a lot easier.

我很确定您的示例符合规则

I'm pretty sure your example falls into the rule no-floating-promises, which disallows the usage of promises without proper handling. In your case bar() returns a promise but there's no await or then to handle the result.

要启用该规则,您必须安装ESLint以及 typescript-eslint 插入.我也强烈建议您在JavaScript上使用TypeScript,因为它会在开发过程中捕获一系列错误,并且您可以将TS代码转换为常见的JS代码.

To enable that rule you'll have to install ESLint plus the typescript-eslint plugin. I'll also strongly recommend you to use TypeScript over JavaScript, since it'll catch a series of bugs during development and you can transpile your TS code into common JS code.

这篇关于在JavaScript中产生新型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆