promise-breaker
makes it easy to write functions that will accept an optional callback, or return
a Promise if a callback is not provided. You can use callbacks or Promises in your implementation,
and callers can call with either a callback or expect a Promise. It's a library that makes it easy
to write libraries for others.
npm install --save promise-breaker
This library assumes that Promise
is a defined global variable. If this is not the case
on your platform, you can use a polyfill:
npm install --save es6-promise
Then somewhere in your node.js application:
if(!global.Promise) {
global.Promise = require('es6-promise').Promise;
}
Or in your client-side app:
if(!window.Promise) {
window.Promise = require('es6-promise').Promise;
}
If you don't want to set the global, you can pass an optional Promise implementation to
promise-breaker
:
var MyPromise = require('es6-promise').Promise;
promiseBreaker = require('promise-breaker').withPromise(MyPromise);
With the growing popularity of Promises these days, if you're a library author, it's nice to be able to provide your clients with a library that will take an optional callback, and if the callback isn't provided, return a Promise. If you've ever tried to do this, you know that there's a lot of finicky boilerplate involved in every function you write. Providing callback support is also pretty important if you prefer to write your library using Promises internally.
'promise-breaker' makes this really easy. If you prefer writing in callback style:
// We're going to make some promises from callbacks
var pb = require('promise-breaker');
exports.myFunc = pb.make(function(done) {
done(null, "Hello World");
});
or if you prefer Promise style:
// We're going to break some promises down into callbacks
var pb = require('promise-breaker')
exports.myFunc = pb.break(function() {
return Promise.resolve("Hello World");
});
No matter which approach you take, users of your library can now call myFunc(done)
, or they
can call myFunc().then(...)
.
make(fn)
takes a function which accepts a callback(err, result)
as its last parameter, and
returns a new function which accepts an optional callback as its last parameter. If a callback is
provided, this new function will behave exactly like the original function. If the callback
is not provided, then the new function will return a Promise.
Since Promises only allow a single value to be returned, if fn
passes more than two arguments to callback(...)
,
then (as of v3.0.0) any arguments after the error will be transformed into an array and returned via the Promise as a
single combined argument. This does not affect the case where the transformed function is called with a callback.
For example:
var myFunc = pb.make(function(callback) {
// We're returning multiple values via callback
callback(null, "a", "b");
})
// Callback style
myFunc(function(err, a, b) {...});
// Promise style
myFunc()
.then(function(results) {
// Promises only let us return a single value, so we return an array.
var a = results[0];
var b = results[1];
...
})
.catch(function(err) {...});
Note that pb.make()
uses fn.length
to determine how many arguments the function expects normally,
so pb.make()
will not work with functions that do not explicitly define their arguments in
their function declaration.
break(fn)
is the opposite of make(fn)
. fn
here is a function which returns a Promise.
break(fn)
will generate a new function with an extra parameter, an optional
callback(err, result)
. If no callback is provided, the generated function will behave exactly
like the original function. If a callback is provided, then the generated function will return
null
, and will pass any results that would have been returned via the Promise via the callback
instead.
Note that pb.break()
uses fn.length
to determine how many arguments the function expects normally,
so pb.break()
will not work with functions that do not explicitly define their arguments in
their function declaration.
Much like Function.prototype.apply()
,
this calls a function, but this lets you call into a function when you don't know whether the
function is expecting a callback or is going to return a Promise. fn
is the function you wish
to call, argumentCount
is the number of arguments you expect the function to take (not including
the callback). Under the hood, this will call fn
and pass in a callback as the
argumentCount + 1
th parameter. If a Promise is returned, applyFn
will assume fn
is Promise
based, otherwise applyFn
will wait for the callback to be called.
If cb
is provided, applyFn
will call into cb
with a result, otherwise applyFn
will itself
return a Promise.
This is the Function.prototype.call()
equivalent of applyFn()
.
Note that if you do not specify an argumentCount
it will default to 0. You can use this handy shortcut:
pb.callFn(function(done) {doSomething(x, y, z, done);})
.then(...)
to call into a callback based function from inside promise-based code.
Returns a new {make, break, applyFn, callFn}
object which uses the specified promiseImpl
constructor to create new Promises.