Convenience methods for converting Node.js callbacks into native Promises.
npm install promise-ring --save
promise-ring is small, simple library with no dependencies that eases the
promise-ringrequires a native
Promiseis not defined, it throws immediately. Therefore, projects that need to support older environments (like Node.js 0.10.x) should look for similar functionality from Q, Bluebird, promise, etc..
For more about using
Promise to write better code, see:
- Promise, Mozilla Developer Network
- Promises, Forbes Lindesay
- Promises/A+, specification
API / Examples
Each of the following snippets comes from the example directory in
the repository and assumes the following
var fs = ;var Database = ;var db = "MockDB";var pr = ;
call wraps a single invocation of a callback-based method and returns a
Promise. If the first (i.e., "error") parameter of the callback is not null,
Promise is rejected. Otherwise, the
Promise is resolved with the second
parameter of the callback. If the callback received multiple parameters, the
Promise is resolved with an
Array of them.
callBound is like
call but allows the
thisArg to be specified for
scenarios that require it (like class instances).
applyBound is to
apply is to
Instead of using
call multiple times for the same function,
wrap creates a
wrapper function that can be used multiple times wherever it is needed.
var readFile = pr;;
wrapBound is to
callBound is to
var dbquery = pr;;
As a convenience,
Promise wrappers for all functions on
an object and binds
thisArg to that object instance (similar to the
wrapAll is not smart and may include functions that don't take a
callback - it's up to the author to ensure only appropriate wrappers are used.
var fsw = pr;var dbw = pr;fsw;dbw;
- 0.1.0 - Initial release.