Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/shellscape/babel-plugin-async-to-plain-generator

Transform async functions into non-wrapped ES2015 generators
https://github.com/shellscape/babel-plugin-async-to-plain-generator

async babel babel-plugin babeljs generator module nodejs npm transformations

Last synced: 21 days ago
JSON representation

Transform async functions into non-wrapped ES2015 generators

Awesome Lists containing this project

README

        

# babel-plugin-async-to-plain-generator

Babel plugin to transform async functions into plain, unwrapped generator functions.

##  


:rocket:   Are you ready to tackle ES6 and hone your JavaScript Skills?   :rocket:

Check out these outstanding ES6 courses by @wesbos


---

While working with [KoaJS v1](https://github.com/koajs/koa) we found that the resulting output from [transform-async-to-generator](https://github.com/babel/babel/tree/master/packages/babel-plugin-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:

```js
// in

[].forEach(async (a) => {
await a;
});

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

// out

[].forEach(function* (a) {
yield a;
});

function* foo (bar) {
yield bar;
}
```

## Installation

```sh
$ npm install babel-plugin-async-to-plain-generator
```

## Usage

### Via `.babelrc` (Recommended)

**.babelrc**

```json
{
"plugins": ["async-to-plain-generator"]
}
```

### Via CLI

```sh
$ babel --plugins async-to-plain-generator script.js
```

### Via Node API

```javascript
require("babel-core").transform("code", {
plugins: ["async-to-plain-generator"]
});
```