[英]Using async/await in Node 6 with Babel
I'm trying to configure Babel for Node v6.9.2. 我正在尝试为Node v6.9.2配置Babel。 I want to use async
/ await
constructs. 我想使用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. Node已经实现了大部分ES6功能。 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. 因此,出于性能原因,我不希望Babel转换Node 6.9.x已经支持的功能(箭头功能,新导入机制等)。
What plugins should I include so I can use async/await? 我应该包含哪些插件,以便我可以使用async / await? There I also confused, because after some researching I found several plugins: syntax-async-functions
, transform-async-to-generator
and some more. 在那里我也很困惑,因为经过一些研究后我发现了几个插件: syntax-async-functions
, transform-async-to-generator
等等。
Example of .babelrc
will help. .babelrc
会有所帮助。
Thanks 谢谢
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
). 预设只是插件的集合,如果您想要转换一组功能(例如所有带有preset-es2015
),它可以更容易使用。 But when you want to transpile only a selection of these features, you only include the corresponding plugins. 但是,当您只想转换这些功能时,您只需要包含相应的插件。
What plugins should I include so I can use async/await? 我应该包含哪些插件,以便我可以使用async / await?
Because Node 6 supports generators, you can use transform-async-to-generator
with the following .babelrc
: 因为节点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. 当然,如果您需要转换更多不受支持的功能,则需要添加插件。
babel-preset-env
另类babel-preset-env
babel-preset-env automatically determines what plugins you need for the specified environment. babel-preset-env自动确定指定环境所需的插件。 This will not include any plugins that are not necessary. 这不包括任何不必要的插件。 To specify your current Node version you would use this .babelrc
: 要指定当前的节点版本,您将使用此.babelrc
:
{
"presets": [
["env", {
"targets": {
"node": "current"
}
}]
]
}
Use Babel preset for Node 6.x: 对节点6.x使用Babel预设:
To see what ES feature is supported in a given Node version, see: 要查看给定节点版本支持的ES功能,请参阅:
For async
/ await
support in particular, see: 有关async
/ await
支持,请参阅:
If you use Node v7.x (the current version) then you can use the --harmony
flag and use async
/ await
natively without transpilation. 如果您使用Node v7.x(当前版本),那么您可以使用--harmony
标志并使用async
/ await
本机而不进行转换。
Node v8.x (available as nightly builds) doesn't even need the --harmony
flag for that. Node v8.x(可用作夜间版本)甚至不需要--harmony
标志。
But note that Node doesn't support import
/ export
- to know why see: 但请注意Node不支持import
/ export
- 要知道为什么看到:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.