Giter Club home page Giter Club logo

babel-plugin-transform-async-to-bluebird's Introduction

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

  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:
{
	"plugins": ["transform-async-to-bluebird"]
}

Credits

This babel plugin is based on babel-helper-remap-async-to-generator

babel-plugin-transform-async-to-bluebird's People

Contributors

chpio avatar karimsa avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.