node package manager
Don’t reinvent the wheel. Reuse code within your team. Create a free org »

promise-light

promise-light - npm

promise-light is standard ES6 Promise implementation.
it supports browser Chrome, Firefox, ie11, ie9, ie8. (tested)
also supports node.js/io.js.

it throws unhandled rejection error.

if you have native Promise then use it.

(faster than native Promise)

INSTALL:

NPM NPM

for node.js or io.js

$ npm install promise-light --save

or

for browsers

https://lightspeedworks.github.io/promise-light/promise-light.js

<script src="https://lightspeedworks.github.io/promise-light/promise-light.js"></script>

PREPARE:

you can use Promise. (native Promise or promise-light)

(function (Promise) {
  'use strict';
  // you can use Promise 
})(typeof Promise === 'function' ? Promise : require('promise-light'));

or

use native Promise or promise-light.

var Promise = this.Promise || require('promise-light');

native Promise is overwritten by promise-light.

var Promise = this.PromiseLight || require('promise-light');

USAGE:

Promise Specification

new Promise(setup)

how to make promise.

= new Promise(
  function setup(resolve, reject) {
    // async process -> resolve(value) or reject(error) 
    try { resolve('value'); }
    catch (error) { reject(error); }
  }
);
// setup( 
//  function resolve(value) {}, 
//  function reject(error) {}) 

example

var p = new Promise(
  function setup(resolve, reject) {
    setTimeout(function () {
      if (Math.random() < 0.5) resolve('value');
      else reject(new Error('error'));
    }, 100);
  }
);
 
p.then(console.info.bind(console),
       console.error.bind(console));

promise.then(onFulfilled, onRejected)

how to use promise.

= p.then(
  function resolved(value) {},
  function rejected(error) {});

example

= p.then(
  function resolved(value) {
    console.info(value);
  },
  function rejected(error) {
    console.error(error);
  });

promise.catch(onRejected)

how to catch error from promise.

= p.catch(
  function rejected(error) {});

or

when you use old browser

= p['catch'](
  function rejected(error) {});

Promise.all(iterable or array)

wait for all promises.

= Promise.all([promise, ...]);

Promise.race(iterable or array)

get value or error of first finished promise.

= Promise.race([promise, ...]);

Promise.resolve(value or promise)

get resolved promise.

= Promise.resolve(value or promise);

Promise.reject(error)

get rejected promise.

= Promise.reject(error);

Promise.accept(value)

get resolved (accepted) promise.

= Promise.accept(value);

Promise.defer()

make deferred object with promise.

dfd = Promise.defer();
// -> {promise, resolve, reject} 

LICENSE:

MIT