使用 Babel.js 转译 Async Await 提案? [英] Transpile Async Await proposal with Babel.js?
问题描述
有一个引入 C# 风格 async-await
的提议.我知道 Babel.js 可以将 ES6 转换为 ES5,但是有没有办法让它将 async-await 转换为 ES5
?
There is a proposal for introducing C# style async-await
. I know Babel.js transpiles ES6 to ES5, but is there any way to make it transpile async-await to ES5
?
推荐答案
Babel v6
从 Babel v6 开始,Babel 本身不再包含任何转换器.您必须明确指定要转换的任何功能.
实现此功能的最快方法是使用 presets,其中已经包含转换 ES2015 和更新提案所需的插件集.对于 async
,您将需要 es2015
和 es2017
预设和 runtime
插件(不要忘记安装 babel-runtime
如文档中所述):
The quickest way to get this working is to use presets which already contain the set of plugins needed to transform ES2015 and newer proposals. For async
, you will need the es2015
and es2017
presets and the runtime
plugin (don't forget to install babel-runtime
as described in the documentation):
{
"presets": [
"es2015",
"es2017"
],
"plugins": [
"transform-runtime"
]
}
预设 - ES2015 环境
如果您在支持 ES2015(更具体地说,生成器和 Promises)的环境中运行代码,那么您只需要 es2017 预设:
Presets - ES2015 environment
If you run the code in an environment that supports ES2015 (more specifically, generators and Promises), then all you need is the es2017 preset:
{
"presets": [
"es2017"
]
}
自定义
要仅转换 async
功能,您将需要以下插件.
Custom
To only transform the async
functions, you will need the following plugins.
syntax-async-functions
是在任何情况下都需要能够解析异步函数
syntax-async-functions
is needed in any every case to be able to parse async functions
为了运行异步函数,你要么需要使用
In order to run the async function, you either need to use
transform-async-to-generator
:将async
函数转换为生成器.这将使用 Babel 自己的协程"实现.transform-async-to-module-method
:同样将async
函数转换为生成器,但传递给配置中指定的模块和方法,而不是 Babel 自己的方法.这允许您使用外部库,例如bluebird
.
transform-async-to-generator
: Converts theasync
function into a generator. This will use Babel's own "co-routine" implementation.transform-async-to-module-method
: Also converts theasync
function to a generator, but passes it to the module and method specified in the configuration instead of Babel's own method. This allows you to use external libraries such asbluebird
.
如果您的代码在支持生成器的环境中运行,那么就没有什么可做的了.但是,如果目标环境不支持生成器,您还必须转换生成器.这是通过 transform-regenerator
转换完成的.这个转换依赖于运行时函数,所以你还需要 Babel 的 transform-runtime
转换(+ babel-runtime
包).
If your code runs in an environment that supports generators, then there is nothing left to do. However, if the target environment does not support generators, you will also have to transform the generator. This is done via the transform-regenerator
transform. This transform depends on runtime functions, so you will also need Babel's transform-runtime
transform (+ the babel-runtime
package).
异步生成器
{
"plugins": [
"syntax-async-functions",
"transform-async-to-generator"
]
}
异步到模块方法
{
"plugins": [
"syntax-async-functions",
["transform-async-to-module-method", {
"module": "bluebird",
"method": "coroutine"
}]
]
}
异步生成器 + 再生器
{
"plugins": [
"syntax-async-functions",
"transform-async-to-generator",
"transform-regenerator",
"transform-runtime"
]
}
<小时>
Babel v4 及更早版本
是的,您必须启用实验转换器.Babel 使用 regenerator.
Babel v4 and older
Yes, you have to enable the experimental transformers. Babel uses regenerator.
使用
$ babel --experimental
<小时>
babel.transform("code", { experimental: true });
这篇关于使用 Babel.js 转译 Async Await 提案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!