Javascript 在 Node 6 中使用 async/await 和 Babel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42299393/
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
Using async/await in Node 6 with Babel
提问by MyTitle
I'm trying to configure Babel for Node v6.9.2. I want to use async/awaitconstructs.
我正在尝试为 Node v6.9.2 配置 Babel。我想使用async/await构造。
Because I'm new to Babel and all Node infrastructure, I confused how to configure it properly:
因为我是 Babel 和所有 Node 基础设施的新手,所以我对如何正确配置它感到困惑:
What preset should I use? Node is already implemented most of the ES6 features. So I don't want Babel to transpile features already supported by Node 6.9.x (arrow functions, new import mechanism etc) for performance reasons.
What plugins should I include so I can use async/await? There I also confused, because after some researching I found several plugins:
syntax-async-functions,transform-async-to-generatorand some more.
我应该使用什么预设?Node 已经实现了大部分 ES6 特性。因此,出于性能原因,我不希望 Babel 转译 Node 6.9.x 已经支持的功能(箭头函数、新的导入机制等)。
我应该包含哪些插件才能使用 async/await?在那里我也很困惑,因为经过一些研究,我发现了几个插件:
syntax-async-functions,transform-async-to-generator还有一些。
Example of .babelrcwill help.
的例子.babelrc会有所帮助。
Thanks
谢谢
采纳答案by Michael Jungo
What preset should I use?
我应该使用什么预设?
You don't need to use any preset. Presets are just a collection of plugins which makes it easier to use if you want to transpile a set of features (for instance all ES2015 with preset-es2015). But when you want to transpile only a selection of these features, you only include the corresponding plugins.
您不需要使用任何预设。预设只是一组插件,如果您想转换一组功能(例如所有带有 的 ES2015 preset-es2015),它会更容易使用。但是,当您只想转译这些功能的一部分时,您只需包含相应的插件。
What plugins should I include so I can use async/await?
我应该包含哪些插件才能使用 async/await?
Because Node 6 supports generators, you can use transform-async-to-generatorwith the following .babelrc:
由于 Node 6 支持生成器,您可以使用transform-async-to-generator以下内容.babelrc:
{
"plugins": ["transform-async-to-generator"]
}
And of course you would need to add plugins if you need to transpile more unsupported features.
当然,如果您需要转译更多不受支持的功能,则需要添加插件。
Alternative babel-preset-env
选择 babel-preset-env
babel-preset-envautomatically determines what plugins you need for the specified environment. This will not include any plugins that are not necessary. To specify your current Node version you would use this .babelrc:
babel-preset-env 会自动确定指定环境所需的插件。这将不包括任何不必要的插件。要指定您当前的 Node 版本,您可以使用.babelrc:
{
"presets": [
["env", {
"targets": {
"node": "current"
}
}]
]
}
回答by rsp
Short answer
简答
Use Babel preset for Node 6.x:
为 Node 6.x 使用 Babel 预设:
Long answer
长答案
To see what ES feature is supported in a given Node version, see:
要查看给定 Node 版本支持哪些 ES 功能,请参阅:
For async/awaitsupport in particular, see:
特别是对于async/await支持,请参阅:
If you use Node v7.x (the current version) then you can use the --harmonyflag and use async/awaitnatively without transpilation.
如果您使用 Node v7.x(当前版本),那么您可以使用该--harmony标志并在本机使用async/await无需转译。
Node v8.x (available as nightly builds) doesn't even need the --harmonyflag for that.
Node v8.x(可用于每晚构建)甚至不需要--harmony标志。
But note that Node doesn't support import/export- to know whysee:
但请注意,Node 不支持import/ export- 要知道为什么请参阅:

