babel-plugin-async-to-plain-generator
While working with KoaJS v1 we found that the resulting output from transform-async-to-generator was incompatible with Koa's requirements - that app.use
must be passed a generator function.
This plugin was created by modifying the transform-async-to-generator plugin. While that plugin works well in most situations, it curiously (and extraneously?) wraps async functions and results in a generator function wrapped in a regular function of the same name. This plugin was created so that babel would produce plain old vanilla generator functions from async function declarations.
Transformations:
// in
[].forEach(async (a) => {
await a;
});
async function foo (bar) {
await bar;
};
// out
[].forEach(function* (a) {
yield a;
});
function* foo (bar) {
yield bar;
}
Installation
$ npm install babel-plugin-async-to-plain-generator
Usage
.babelrc
(Recommended)
Via .babelrc
{
"plugins": ["async-to-plain-generator"]
}
Via CLI
$ babel --plugins async-to-plain-generator script.js
Via Node API
require("babel-core").transform("code", {
plugins: ["async-to-plain-generator"]
});