babel-plugin-transform-async-to-bluebird
v1.1.1
Published
Transforms *async* to *bluebird*
Downloads
1,040
Readme
babel-plugin-transform-async-to-bluebird
This plugin transforms async
to bluebird
.
Examples
An async function containing no await gets wrapped by bluebirds method function without using the generator, resulting in less overhead. All returned or thrown values are wrapped by Promises and then returned.
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 bluebirds coroutine; it's using the generator.
async function myFunction(input) {
if (input === 0) throw new TypeError('Invalid input');
const res = await promiseReturningFunction(input);
return res + 10;
}
Usage
- Install bluebird:
npm install --save bluebird
- Install the plugin:
npm install --save-dev babel-plugin-transform-async-to-bluebird
- Add transform-async-to-bluebird to your .babelrc file:
{
"plugins": ["transform-async-to-bluebird"]
}
Credits
This babel plugin is based on babel-helper-remap-async-to-generator