babel-plugin-bluebird-async-functions
Wrap async methods with Bluebird's Coroutine and Method helpers
Last updated 5 years ago by sportula .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install babel-plugin-bluebird-async-functions 
SYNC missed versions from official npm registry.

babel-plugin-bluebird-async-functions

Convert async methods into Bluebird's Coroutine and Method helpers by wrapping methods marked as 'async' with the Promise.method method, and async methods containing 'await' expressions with the Promise.coroutine.

Dependencies

This plugin requires the babel-plugin-syntax-async-functions plugin to be loaded before it in order for Babel to properly parse async method definitions.

Installation

Install with NPM:

npm install babel-plugin-bluebird-async-functions

Reference this plugin (with the babel-plugin-syntax-async-functions plugin first) in your .babelrc file:

{
  "presets": ["es2015"],
  "plugins": [
    "babel-plugin-syntax-async-functions",
    "babel-plugin-bluebird-async-functions"
  ]
}

#Example sample.js

class MyClass {

  // async method
  async getData() {  
    return Promise.resolve("data").delay(500);
  }

  // async generator
  async getLotsOfData() {
    let datas = [];
    for (let i = 0; i < 10; i++) {
      let datum = await Promise.resolve(i).delay(500);
      datas.push(datum);
    }
    return datas;
  }
}

babel sample.js:

"use strict";

var _createClass = function () { /* Babel create class method */ }();

function _classCallCheck(instance, Constructor) { /* Babel class check stuff */ }

var MyClass = function () {
  function MyClass() {
    _classCallCheck(this, MyClass);
  }

  _createClass(MyClass, [{
    key: "getData",

    // async method
    value: Promise.method(function getData() {
      return Promise.resolve("data").delay(500);
    })

    // async generator

  }, {
    key: "getLotsOfData",
    value: Promise.coroutine(regeneratorRuntime.mark(function getLotsOfData() {
      var datas, i, datum;
      return regeneratorRuntime.wrap(function getLotsOfData$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              datas = [];
              i = 0;

            case 2:
              if (!(i < 10)) {
                _context.next = 10;
                break;
              }

              _context.next = 5;
              return Promise.resolve(i).delay(500);

            case 5:
              datum = _context.sent;

              datas.push(datum);

            case 7:
              i++;
              _context.next = 2;
              break;

            case 10:
              return _context.abrupt("return", datas);

            case 11:
            case "end":
              return _context.stop();
          }
        }
      }, getLotsOfData, this);
    }))
  }]);

  return MyClass;
}();

Worth Mentioning

For browser use, ensure that the Bluebird library is loaded (for obvious reasons), and in a Node environment that it has been loaded globablly or within the current script.

Current Tags

  • 1.2.0                                ...           latest (5 years ago)

8 Versions

  • 1.2.0                                ...           5 years ago
  • 1.1.3                                ...           5 years ago
  • 1.1.2                                ...           5 years ago
  • 1.1.1                                ...           5 years ago
  • 1.1.0                                ...           5 years ago
  • 1.0.2                                ...           5 years ago
  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (4)
Dev Dependencies (1)
Dependents (2)

Copyright 2014 - 2016 © taobao.org |