babel-plugin-promise-to-q

1.0.3 • Public • Published

babel-plugin-promise-to-q

Turns native promises into angular $q service

Build Status

Why?

Babel uses native browser promises, which in most cases are perfectly fine, but they can cause some problems if you are using babel with Angular 1. Since Angular already relies on the $q service, this plugin converts the native browser promises to also use the $q service. This eliminates $digest problems that happen when mixing the two types of promises.

Example

In

new Promise(function(resolve, reject) {
  try {
    if (something) {
      resolve(true);
    } else {
      resolve(false);
    }
  } catch (error) {
    reject(error);
  }
});

Out

$q(function(resolve, reject) {
  try {
    if (something) {
      resolve(true);
    } else {
      resolve(false);
    }
  } catch (error) {
    reject(error);
  }
});

Installation

$ npm install babel-plugin-promise-to-q

Usage

Via .babelrc (Recommended)

.babelrc

{
  "plugins": ["promise-to-q"]
}

Via CLI

$ babel --plugins promise-to-q script.js

Via Node API

require("babel-core").transform("code", {
  plugins: ["promise-to-q"]
});

Package Sidebar

Install

npm i babel-plugin-promise-to-q

Weekly Downloads

6

Version

1.0.3

License

none

Last publish

Collaborators

  • justinfuller