Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chpio/babel-plugin-transform-async-to-bluebird
https://github.com/chpio/babel-plugin-transform-async-to-bluebird
Last synced: about 13 hours ago
JSON representation
- Host: GitHub
- URL: https://github.com/chpio/babel-plugin-transform-async-to-bluebird
- Owner: chpio
- Created: 2016-07-07T05:18:34.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-06-30T09:35:51.000Z (over 7 years ago)
- Last Synced: 2024-09-19T15:36:26.311Z (about 2 months ago)
- Language: JavaScript
- Size: 10.7 KB
- Stars: 7
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# babel-plugin-transform-async-to-bluebird
This plugin transforms `async` to `bluebird`.
## Examples
An *async* function containing **no** *await* gets wrapped by *bluebird*s
[*method*](http://bluebirdjs.com/docs/api/promise.method.html) function without
using the generator, resulting in less overhead. All returned or thrown values
are wrapped by *Promis*es and then returned.
```javascript
async function myFunction(input) {
if (input === 0) throw new TypeError('Invalid input');
if (input === 42) return promiseReturningFunction(input);
return input + 10;
}myFunction(0); // Returns a Promise, rejecting into an TypeError('Invalid input')
myFunction(42); // Returns a Promise
myFunction($other_value); // Returns a Promise, resolving into $other_value + 10
```On the other side an *async* + *await* function gets wrapped by *bluebird*s
[*coroutine*](http://bluebirdjs.com/docs/api/promise.coroutine.html); it's
using the generator.```javascript
async function myFunction(input) {
if (input === 0) throw new TypeError('Invalid input');
const res = await promiseReturningFunction(input);
return res + 10;
}
```## Usage
1. Install *bluebird*: `npm install --save bluebird`
2. Install the plugin: `npm install --save-dev babel-plugin-transform-async-to-bluebird`
3. Add *transform-async-to-bluebird* to your *.babelrc* file:
```json
{
"plugins": ["transform-async-to-bluebird"]
}
```## Credits
This babel plugin is based on
[babel-helper-remap-async-to-generator](https://github.com/babel/babel/tree/master/packages/babel-helper-remap-async-to-generator)