lift-p
v1.0.1
Published
Lift a function expecting a callback into a promise
Downloads
8
Readme
lift-p - Lift functions expecting callbacks into functions returning promises
lift-p takes functions of the form function (a1, a2, ..., an, cb)
and turns them into functions of the form
function (a1, a2, ..., an) -> Promise
.
e.g.
var liftP = require('lift-p');
var assert = require('assert');
liftP(function(a, cb){
setTimeout(function(){
cb(null, a);
}, 20);
})('foo')
.then(function(v){
assert(v === 'foo');
});
If something goes wrong, the promise fails.
liftP(function(cb){
setTimeout(function(){
cb('failure');
}, 20);
})()
.catch(function(e){
assert(e === 'failure');
});
You may also pass in a value for this
that will be bound after all parameters are given.
var liftP = require('lift-p');
var assert = require('assert');
var C = function(v){
this.v = v;
};
C.prototype.run = function(a1, cb) {
var that = this;
setTimeout(function(){
cb(null, that.v + ' ' + a1);
}, 20);
};
var o = new C('foo');
liftP(o.run, o)('bar')
.then(function(v){
assert(v === 'foo bar');
});