Promish
SH
I
P R O M
The Promish module creates a wrapper around the EcmaScript 6 Promise class.
It adds some of the useful features found in many of the other popular promise libraries such as Q and Bluebird.
It is designed to be interchangeable with the ES6 Promise as its interface is a superset of the Promise class.
This is probably one of the smallest promise libraries - currently around 6.5kb (smaller than this README!)
and comes with no dependencies (other than ES6 Promises).
Installation
npm install promish
New Features!
Contents
Interface
Include
var Promish = require('promish');
Instantiation
Typical use - construct with handler function
var promise = new Promish(function(resolve, reject) {
});
3rd Party Wrapper Mode
var promise = new Promish(Q());
var promise = new Promish(new Promise( ... ));
Value Wrapper Mode
var promise = new Promish('Resolve Value');
var promise = Promish.resolve('Resolve Value');
Error Wrapper Mode
var promise = new Promish(new Error('This promise is rejected'));
var promise = Promish.reject('This is not an error object, but reject with it anyway')
Then
promise
.then(function(value) {
});
promise.then(
function(value) {
},
function(error) {
});
Catch
The catch function takes a catch handler that will be called when the promise state is rejected
and is a more elegant way to handle errors than using the second then argument.
promise
.catch(function(error) {
});
Promishes also support Error type matching
new Promish(function(resolve, reject) {
resolve(JSON.parse(text));
})
.then(function(json) { ... })
.catch(SyntaxError, function(error) {
})
.catch(function(error) {
});
And also support user supplied error match functions
function isFooString(value) {
return ((typeof value) === 'string') && (value.indexOf('foo') >= 0);
}
promise
.then(function(value) { ... })
.catch(isFooString, function(error) {
})
.catch(function(error) {
});
Finally
A finally handler will be called no matter what state the promise chain gets into.
There are no arguments provided to the finally handler and the downstream promise state will depend on the handler code.
Typically this will be resolved with the return value (e.g. undefined).
promise
.then(function(value) { ... })
.catch(function(error) { ... })
.finally(function() {
})
Delay
Pause for a number of milliseconds and then continue.
The resolve value will be preserved.
If the promish state is rejected, delay will not delay and will preserve the rejection error
getAPromish()
.delay(1000)
.then(function(value) {
})
.catch(function(error) {
});
Defer
For compatability with the old Promise.defer() pattern...
function readAFile(filename) {
var deferred = Promish.defer();
fs.readFile(filename, function(error, data) {
if (error) {
deferred.reject(error);
} else {
deferred.resolve(data);
}
});
return deferred.promise;
}
Promisification Calls
The majority of the old asynchronous Node methods follow a basic pattern where the last argument in a function
is a callback function and the first argument of that callback function is used to signal errors -
if the error argument is truthy, then the call failed and the value of the error will indicate why,
otherwise the call succeeded.
Promisification involves converting the async pattern into promises - either on the fly or by wrapping functions,
methods or even whole objects...
Apply
Promish.apply(fs.readFile, [filename])
.then(function(data) {
})
.catch(function(error) {
});
Call
Promish.call(fs.readFile, filename)
.then(function(data) {
})
.catch(function(error) {
});
Post
Promish.invoke(target, value1, value2)
.then(function(value) { ... });
Invoke
Promish.invoke(target, [value1, value2])
.then(function(value) { ... });
Promisify
Convert a function from async to promise for future use.
var readFile = Promish.promisify(fs.readFile);
readFile(filename)
.then(function(data) { ... })
Promisify All
Promisify all the async methods of an object.
There are two modes supported:
- Proxy Mode (default)
- Creates a separate object that contains promisified methods for each method of the target object. The methods typically have the same name
- Note: ES6 Proxies eagerly awaited here!
- In-Place Mode
- Adds promisified methods to the object, typically with a suffix to avoid colliding with the actual methods.
var fs = Promish.promisifyAll(require('fs'));
fs.readFile(filename)
.then(function(data) { ... });
var fs = Promish.promisifyAll(require('fs'), { inPlace: true, suffix: 'Async' });
fs.readFileAsync(filename)
.then(function(data) { ... });
Method
Wrap a synchronous function or method so that it always returns a promise
var myFunc = Promish.method(function(value) {
if (!value) throw new Error('Not zero!');
if (value > 0) return value;
return Promish.resolve(value);
});
myFunc(1234)
.then(function(value) {
});
MyClass.prototype.func = Promish.method(function(value) {
return this.value = value;
});
new MyClass(7).func
.then(function(value) {
});
All
Promish wraps the native implementation of all.
Promish.all([getPromise1(), getPromise2()])
.then(function(values) { ... });
Race
Promish wraps the native implementation of race.
Promish.race([promise1, promise2])
.then(function(value) {
})
.catch(function(error) {
});
Some
Resolve on first N successful promises or reject with array of errors.
Promish.some([promise1, promise2, promise3], 2)
.then(function(values) {
})
.catch(function(errors) {
});
Any
Resolve on first successful promise or reject with array of errors.
Promish.any([promise1, promise2])
.then(function(value) {
})
.catch(function(errors) {
});
Spread
Convert a resolve value array into arguments
Promish.all([getPromish1(), getPromish2(), getPromish3()])
.spread(function(a,b,c) {
});
Spread will also convert an array of promises into their resolved values
new Promish(function(resolve) {
resolve([getPromish1(), getPromish2(), getPromish3()])
})
.spread(function(a,b,c) {
// a === value from getPromish1
// b === value from getPromish2
// c === value from getPromish3
});
Known Issues
Release History
Version | Changes |
---|
0.0.1 | |
0.0.2 | |
0.0.3 | |
0.0.4 | |
0.0.5 | |
0.0.6 | - Bugfixes and Documentation
|
4.2.2 | - Updated for compatability with Node v4.
- Version number matches the version of Node I used when this was published
- For compatability with older 0.x versions, specify Promish version 0.0.8 in your package.json
- Refactored to extend Promise instead of wrapping
|
4.2.3 | - Added implict Promish.all to spread
|
4.2.4 | - finally() must not swallow rejection.
|
4.2.5 | |