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

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

Example

In

  1. async function foo() {
  2. await bar();
  3. }

Out

  1. var _asyncToGenerator = function (fn) {
  2. ...
  3. };
  4. var foo = _asyncToGenerator(function* () {
  5. yield bar();
  6. });

Out with options

Turn async functions into a Bluebird coroutine

  1. var Bluebird = require("bluebird");
  2. var foo = Bluebird.coroutine(function* () {
  3. yield bar();
  4. });

Installation

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

Usage

.babelrc

Without options:

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

With options:

  1. {
  2. "plugins": [
  3. ["@babel/plugin-transform-async-to-generator", {
  4. "module": "bluebird",
  5. "method": "coroutine"
  6. }]
  7. ]
  8. }

Via CLI

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

Via Node API

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

References