Babel v6
As for Babel v6, Babel no longer contains transformers. You must explicitly specify any function that you want to convert.
Presets - Environment without ES2015
The fastest way to get this working is to use presets that already contain the set of plugins needed to convert ES2015 and new offers. For async you will need the preliminary es2015 and es2017 and the runtime plugin (do not forget to set the babel-runtime as described in the documentation):
{ "presets": [ "es2015", "es2017" ], "plugins": [ "transform-runtime" ] }
Presets - ES2015 Environment
If you run the code in an environment that supports ES2015 (more specifically, generators and promises), then you only need to install es2017:
{ "presets": [ "es2017" ] }
custom
To convert only async functions, you will need the following plugins.
syntax-async-functions necessary in any case for the possibility of parsing asynchronous functions
To run the async function, you need to either use
transform-async-to-generator : Converts an async function to a generator. This will use the โcollaborativeโ implementation of Babel.transform-async-to-module-method : also converts the async function to a generator, but passes it to the module and method specified in the configuration instead of the Babel native method. This allows the use of external libraries such as bluebird .
If your code runs in an environment that supports generators, then nothing remains. However, if the target environment does not support generators, you will also have to convert the generator. This is done using transform-regenerator . This conversion depends on the runtime functions, so you will also need transform-runtime Babel (+ babel-runtime package).
Examples:
Asynchronous generator
{ "plugins": [ "syntax-async-functions", "transform-async-to-generator" ] }
Asynchronous method
{ "plugins": [ "syntax-async-functions", ["transform-async-to-module-method", { "module": "bluebird", "method": "coroutine" }] ] }
Asynchronous generator + regenerator
{ "plugins": [ "syntax-async-functions", "transform-async-to-generator", "transform-regenerator", "transform-runtime" ] }
Babel v4 and older
Yes, you must turn on experimental transformers . Babylon uses a regenerator .
Application
$ babel --experimental
babel.transform("code", { experimental: true });