babel/packages/babel-plugin-transform-async-to-generator
Gvozd 653318b7e4 Always transform for-await in async functions. (#7446)
for-await was transformed in @babel/helper-remap-async-to-generator, which was
called by @babel/plugin-transform-async-to-generator and
@babel/plugin-proposal-async-generator-functions. This prevented for-await
statements in async functions to be transpiled if the
transform-async-to-generator plugin was't enabled.
2018-03-05 09:30:25 +01:00
..
2017-03-25 21:46:16 -04:00

@babel/plugin-transform-async-to-generator

Turn async functions into ES2015 generators

In Babel 7, transform-async-to-module-method was merged into this plugin

Example

In

async function foo() {
  await bar();
}

Out

var _asyncToGenerator = function (fn) {
  ...
};
var foo = _asyncToGenerator(function* () {
  yield bar();
});

Out with options

Turn async functions into a Bluebird coroutine

var Bluebird = require("bluebird");

var foo = Bluebird.coroutine(function* () {
  yield bar();
});

Installation

npm install --save-dev @babel/plugin-transform-async-to-generator

Usage

.babelrc

Without options:

{
  "plugins": ["@babel/plugin-transform-async-to-generator"]
}

With options:

{
  "plugins": [
    ["@babel/plugin-transform-async-to-generator", {
      "module": "bluebird",
      "method": "coroutine"
    }]
  ]
}

Via CLI

babel --plugins @babel/plugin-transform-async-to-generator script.js

Via Node API

require("@babel/core").transform("code", {
  plugins: ["@babel/plugin-transform-async-to-generator"]
});

References