Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including promise-light with all npm packages installed. Try it out:

var promiseLight = require("promise-light")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

promise-light v0.1.14

promise light version

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.

p = 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 = p.then(
  function resolved(value) {},
  function rejected(error) {});

example

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

promise.catch(onRejected)

how to catch error from promise.

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

or

when you use old browser

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

Promise.all(iterable or array)

wait for all promises.

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

Promise.race(iterable or array)

get value or error of first finished promise.

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

Promise.resolve(value or promise)

get resolved promise.

p = Promise.resolve(value or promise);

Promise.reject(error)

get rejected promise.

p = Promise.reject(error);

Promise.accept(value)

get resolved (accepted) promise.

p = Promise.accept(value);

Promise.defer()

make deferred object with promise.

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

LICENSE:

MIT

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free