Skip to content

Extends any Promises/A+ compliant implementation with extra utilities

Notifications You must be signed in to change notification settings

UmbraEngineering/promise-extensions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 

Repository files navigation

Promise Extensions

Extensions to help when working with a Promises/A+ implementation.

Need promises? Maybe take a look at promise-es6.

Install

$ npm install [--save] promise-extensions

Usage

require('promise-extensions').init(Promise)
    .install('defer');

// Use the newly installed defer function
var deferred = Promise.defer();

doTheThing(function() {
    deferred.resolve('foo!');
});

deferred.promise.then(function(foo) {
    // ...
});

Extensions

Promise.all ( Array promises )

A compliant Promise.all implementation. Returns a new promise that resolves once all of the given promises resolve.

require('promise-extensions').init(Promise).install('all');

Promise.all([ promise1, promise2, promise3 ])
    .then(function(results) {
        //
    });

Promise.defer ( void )

Returns a deferred object containing a new promise, and a resolve and reject method.

require('promise-extensions').init(Promise).install('defer');

var deferred = Promise.defer();

deferred.promise.then(function(foo) {
    // ...
});

deferred.resolve('foo');

Promise.guard ([ Number limit,] Function func )

Returns a new function with limited concurrency. The new function will only run a maximum of limit instances at a time.

require('promise-extensions').init(Promise).install('guard');

var func = Promise.guard(3, function() {
    return new Promise(function(resolve, reject) {
        setTimeout(resolve, Math.random() * 1000);
    });
});

for (var i = 0; i < 10; i++) {
    func();
}

Promise.ify ( Function func )

Returns a new version of a node-style async function that returns a promise.

require('promise-extensions').init(Promise).install('ify');

var nodeStyle = function(foo, bar, callback) {
    setTimeout(function() {
        callback(null, 'success!');
    }, 1000);
};

var promiseStyle = Promise.ify(nodeStyle);

promiseStyle('foo', 'bar')
    .then(function(result) {
        console.log(result);
    })
    .catch(function(err) {
        console.error(err);
    });

Promise.race ( Array promises )

Promise.wait ( Number ms )

About

Extends any Promises/A+ compliant implementation with extra utilities

Resources

Stars

Watchers

Forks

Packages

No packages published