Wraps an asynchronous function that either takes a callback or returns a promise, and allows it to do both.
Note: Promisebacker is still under development and not yet ready for production systems. Releases are stable but the API is subject to rapidly change.
npm install promisebacker
var Promise = require('promise') // Any Promises/A+ compliant library will do.
, promisebacker = require('promisebacker');
var takes_callback = function(arg1, arg2, callback) {
// Does something...
var error = false;
// Asynchronously invokes callback.
setTimeout(function() {
if (error){
return callback('uh oh!');
}
callback(null, 'hello world!');
}, 3000);
};
var returns_promise = function(arg1, arg2) {
// Does something...
var error = false;
// Returns promise that resolves asynchronously.
return new Promise(function(resolve, reject) {
setTimeout(function() {
if (error) {
return reject('uh oh!');
}
resolve('hello world!');
}, 3000);
});
};
var wrapped_callback = promisebacker(takes_callback);
// Now we can pretend this returns a promise...
wrapped_callback('alas', 'poor yorick')
.then(function(result) {
// result == 'hello world!'
});
// ...or we can continue using it as a callback!
wrapped_callback('alas', 'poor yorick', function(err, result) {
// err == null
// result == 'hello world!'
});
// And we can do the same for functions that return promises.
var wrapped_promise = promisebacker(returns_promise);
wrapped_promise('alas', 'poor yorick')
.then(function(result) {
// result == 'hello world!'
});
wrapped_promise('alas', 'poor yorick', function(err, result) {
// err == null
// result == 'hello world!'
});
Promisebacker(Function target)
assumes that you're trying to use a callback if the last argument passed is a Function
of arity at least 2. If you want to force it to return promises, use Promisebacker.toPromise
instead.
We define a function to take a node-style callback (a nodeback) if it accepts a Function
of arity at least 2 as its last argument and invokes that function whenever it finishes running. When invoking its callback, it must pass an error value as its first argument which must be truthy if and only if an error has occurred.
Wraps target
such that it can either take a callback or return a promise.
target
must either return a promise or take nodebacks.options
is an optional object with options.
If target
takes nodebacks and calls its callback with multiple success values, the fulfillment value will be an array of them.
See the bluebird
documentation for promisification for details.
If you pass a scope
, then target
will have its this
bound to scope
(i.e. as if it were being called as scope.target
).
Some nodebacks expect more than 1 success value but there is no mapping for this in the promise world. If spread
is specified, the nodeback is called with multiple values when the promise fulfillment value is an array:
var example = Promisebacker(Promise.resolve);
example([1, 2, 3], function(err, result) {
// err == null
// result == [1, 2, 3]
});
var another = Promisebacker(Promise.resolve, {spread: true});
another([1, 2, 3], function(err, a, b, c) {
// err == null
// a == 1, b == 2, c == 3
});
Same as above, but will always return a Promise
even if the last argument is a Function
of arity at least 2.
© 2014 Lehao Zhang. Released under the terms of the MIT license.