Permalink
Fetching contributors…
Cannot retrieve contributors at this time
93 lines (66 sloc) 1.36 KB
---
id: babel-plugin-transform-async-to-generator
title: @babel/plugin-transform-async-to-generator
sidebar_label: transform-async-to-generator
---

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

Via .babelrc (Recommended)

.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