Javascript JSHint 是否支持异步/等待?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42637630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does JSHint support async/await?
提问by Alena Gilevskaya
I'm using JSHint for the JavaScript project (with the Visual Studio Code). And in this project I use async / await, which JSHint highlights as errors.
我将 JSHint 用于 JavaScript 项目(使用 Visual Studio Code)。在这个项目中,我使用了 async/await,JSHint 将其突出显示为错误。
I tried to set up jshint, but the it seems like the maxim version of "esversion" is 6.
我试图设置 jshint,但似乎“esversion”的格言版本是 6。
Does jshint support async/await yet? If it does, how to turn it on? And if not, are there any workarounds?
jshint 支持 async/await 吗?如果可以,如何开启?如果没有,是否有任何解决方法?
回答by colxi
Update (February 2019): Async/await are now supported as of version 2.10.1. Simply update your .jshintrc to use
"esversion": 9. (+Info : Version changelog)
更新(2019 年 2 月):从 2.10.1 版开始,现在支持 Async/await。只需更新您的 .jshintrc 即可使用
"esversion": 9. (+信息:版本更新日志)
Update (july 2018): Async/await will arrive with the release of JsHint 2.10.0. +info : https://github.com/jshint/jshint/pull/3273
更新(2018 年 7 月):Async/await 将随着 JsHint 2.10.0 的发布而到来。+信息:https: //github.com/jshint/jshint/pull/3273
The JSHINT developing community considers that:
JSHINT 开发社区认为:
- JSHINT should first support all the ES6 syntax before start implementing ES7 features.
- Async Functions are only at stage 1, so the syntax can change a lot
- 在开始实现 ES7 特性之前,JSHINT 应该首先支持所有 ES6 语法。
- 异步函数仅处于第 1 阶段,因此语法可能会发生很大变化
JSHINT-ESNEXT (PACKAGE)
JSHINT-ESNEXT(包)
However, there is an unnoficial JSHINT package, wich contains experimental support for await/async, called JSHINT-ESNEXT.
但是,有一个非官方的 JSHINT 包,其中包含对 await/async 的实验性支持,称为JSHINT-ESNEXT。
The author, @marcominetti, used the official JSHint 2.7 Master branch and introduced this ES7 feature, taken from the Seb Vincent esnextnextbranch.
作者@marcominetti使用了官方的JSHint2.7 Master 分支并引入了这个 ES7 特性,取自Seb Vincent esnext分支。
Check the Npm Package, and the source in github
Installation: $npm install -g jshint-esnext
安装:$npm install -g jshint-esnext
Right now (Jul 2017) this is the only decent available approach to support await/async in JSHINT.
现在(2017 年 7 月)这是在 JSHINT 中支持 await/async 的唯一合适的可用方法。
JSHINT IGNORE (DIRECTIVE)
JSHINT 忽略(指令)
A common suggested workaround or mitigation practice, is using the JSHINT ignore directives.
一个常见的建议解决方法或缓解措施是使用 JSHINT 忽略指令。
// Code here will be linted with JSHint.
/* jshint ignore:start */
// Code here will be ignored by JSHint.
/* jshint ignore:end */
Or:
或者:
ignoreThis(); // jshint ignore:line
I personally find this mitigation practice, dirty and confusing, when our code has a big amount of async/await references. But more confusing and dirty are the JSHINT warnings and errors ;)
当我们的代码有大量 async/await 引用时,我个人认为这种缓解实践既肮脏又令人困惑。但更令人困惑和肮脏的是 JSHINT 警告和错误;)
回答by jfriend00
Does jshint support async/await yet?
jshint 支持 async/await 吗?
No, not yet as of early 2017.
不,截至 2017 年初还没有。
It appears that it does not yet support async/await. Folks working on jsHint have decided not to support async/awaituntil standards were in a late stage (having apparently been burned previously by supporting things too early when they were still changing) - though implementations already exist (Babel, nodejs, etc...).
好像还不支持async/await。在 jsHint 上工作的人们决定async/await在标准处于后期阶段之前不支持(显然之前因为在它们仍在更改时过早地支持事物而被烧毁) - 尽管实现已经存在(Babel,nodejs 等......)。
If you follow this jsHint open issue thread, it is still an open issue as of recent comments 7 and 11 days ago.
如果您关注此jsHint 开放问题线程,则截至 7 天和 11 天前的最新评论,它仍然是一个未解决的问题。
And if not, are there any workarounds?
如果没有,是否有任何解决方法?
As of July 2017, there appears to be a fork of the jsHint code called jshint-esnext that has support for async/await here: https://www.npmjs.com/package/jshint-esnext.
截至 2017 年 7 月,似乎有一个名为 jshint-esnext 的 jsHint 代码分支,支持 async/await:https://www.npmjs.com/package/jshint-esnext 。
回答by JoelABair
Note: Using the forked JSHINT-ESNEXT package (NPM, and github) as suggested in the "accepted answer" requires the inclusion of the "experimental" option to be set.
注意:使用“接受的答案”中建议的分叉 JSHINT-ESNEXT 包(NPM和github)需要包含要设置的“实验”选项。
/* experimental: [asyncawait] */
/* experimental: [asyncawait] */
or
或者
/* experimental: [asyncawait, asyncreqawait] */
/* experimental: [asyncawait, asyncreqawait] */
See the source code herefor details.
有关详细信息,请参阅此处的源代码。
回答by JohnN
The OP asked it there were any workarounds. It's a kludge, but I've replaced all my 'async' or 'await' with 'async /**/' and 'await /**/'. Then a quick script to swap them into '/*async*/' and '/*await*/' allows me to check with jshint.
OP 询问是否有任何解决方法。这是一个混乱,但我已经用'async /**/'和'await /**/'替换了我所有的'async'或'await'。然后一个快速脚本将它们交换成 '/*async*/' 和 '/*await*/' 允许我检查 jshint。
It's not pretty, but it feeds the bulldog.
它不漂亮,但它喂养斗牛犬。

