Extensions to help when working with a Promises/A+ implementation.
Need promises? Maybe take a look at promise-es6.
$ npm install [--save] promise-extensions
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) {
// ...
});
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) {
//
});
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');
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();
}
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);
});