在节点6中使用带有Babel的async / await [英] Using async/await in Node 6 with Babel

查看:772
本文介绍了在节点6中使用带有Babel的async / await的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Node v6.9.2配置Babel。我想使用 async / 等待构造。

I'm trying to configure Babel for Node v6.9.2. I want to use async/await constructs.

因为我是Babel和所有Node基础架构的新手,所以我很困惑如何正确配置它:

Because I'm new to Babel and all Node infrastructure, I confused how to configure it properly:


  • 我应该使用什么预设? Node已经实现了大部分ES6功能。因此,出于性能原因,我不希望Babel转换Node 6.9.x已经支持的功能(箭头功能,新导入机制等)。

  • 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.

我应该包含哪些插件,以便我可以使用async / await?在那里我也很困惑,因为经过一些研究我发现了几个插件: syntax-async-functions transform-async-to-generator 等等。

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-generator and some more.

.babelrc 的示例会有所帮助。

Example of .babelrc will help.

谢谢

推荐答案


预设我应该使用吗?

What preset should I use?

您不需要使用任何预设。预设只是插件的集合,如果您想要转换一组功能(例如所有ES2015都带有预设-es2015 ),它就更容易使用。但是当你想要只转换这些功能的选择时,你只需要包含相应的插件。

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.


我应该包含哪些插件,以便我可以使用async / await?

What plugins should I include so I can use async/await?

因为Node 6支持生成器,所以你可以使用 transform-async-to-generator 以下 .babelrc

Because Node 6 supports generators, you can use transform-async-to-generator with the following .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 自动确定指定环境所需的插件。这不包括任何不必要的插件。要指定当前的节点版本,您将使用此 .babelrc

babel-preset-env automatically 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:

{
  "presets": [
    ["env", {
      "targets": {
        "node": "current"
      }
    }]
  ]
}

这篇关于在节点6中使用带有Babel的async / await的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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