babel/packages/babel-plugin-transform-async-to-generator
Nicolò Ribaudo 05b22d2597
Update @babel/helper-wrap-function templates (#6984)
This commit introduces 4 changes:

1) Function declarations are wrapped using function declarations.
   This has two advantages:
    - We can rely on native hoisting, instead of using _blockHoist
    - The function isn't wrapped until it is called. This avoids
      problems where `regeneratorRuntime.wrap` was called before
      that `babel-polyfill` was imported.

   Example:
     function fn() {}
     // becomes
     function fn() { return _fn.apply(this, arguments); }
     function _fn() {
       _fn = _wrapper(/* Original function ... */);
       return _fn.apply(this, arguments);
     }

2) Use a single template for both named and anonymous function
   expressions. They already had the same behavior, but the one
   used for named functions was a bit longer.

3) Use normal functions instead of arrow functions to wrap
   function expressions.

4) Generate a name based on the original one for wrapped
   functions (e.g. `foo` becomes `_foo` instead of `_ref`).
2017-12-13 16:21:58 +01:00
..
2017-03-25 21:46:16 -04:00
2017-12-02 09:38:52 -05: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