polyfill-promise

polyfill standard Promises to old versions of node

polyfill-promise

Use this to ensure the Promise constructor is available, even in old versions of node (< 0.12) or browsers

var Promise = require('polyfill-promise')
// either native or polyfilled 
 
var foo = Promise.resolve('foo')
foo.then(...)

or

require('polyfill-promise/register')
// global Promise constructor is now available 
var foo = Promise.resolve('foo')
foo.then(...)

If polyfill is used, this read-only property is set to true. If a native implementation is available, this property is undefined.

additionally, implements the Promise api. Quick reference:

Creates a pending promise. Pass it a factory function which wraps the asynchronous operation. factory is called with two functions, reject and resolve which should be called when the asynchronous operation is complete or an exception ocurrs.

Example: delay a value by an amount of time

function delay(valuetimeout) {
  return new Promise(function (resolvereject) {
    setTimeout(function () {
      resolve(value)
    }, timeout)
  })
}

Example: wrap a node-style callback

var result = new Promise(function (resolvereject) {
  asyncFn(function (errresult) {
    if (err) { reject(err); return }
    resolve(result)
  })  
})

Create a Promise which is immediately rejected with an optional reason.

Create a Promise which is immediately resolved with an optional val.

Returns a Promise which is resolved when all of the Promises in the promises array are resolved, or rejected if any of them is rejected. The resolved value is an Array of the resolved values in the same order.

Returns a Promise which is resolved as soon as the first Promise in the promises array is resolved, or rejected when the first Promise in the promises array is rejected. Since a Promise can only be either resolved or rejected but not both, if one Promise in promises is resolved and then another one is rejected, the resulting Promise will still be resolved and will ignore the later rejection.

Chains a function fn on the Promise to be evaluated when the Promise is resolved. The return value of fn is resolved to a new Promise which allows for chaining and composition.

An optional second parameter is an error handling function which is called if the Promise is rejected.

Chains an error handling function to a Promise chain which will be called if a Promise is rejected. Returns a new Promise.

$ npm test

none yet

by jden jason@denizac.org, ISC license