resolver-macro
v0.1.0
Published
A sweet.js macro for creating a promise-resolving function.
Downloads
1
Readme
resolver-macro
This sweet.js macro will help you turn any standard callback-based function into a promise, using whichever promise library you want.
Example
Here's an example using when and the standard fs.readFile
command:
when.promise( resolver(fs.readFile, 'hello_world.txt', {encoding: 'utf-8'}) );
Getting Started
To use the macro, take a look at the sweet.js project, or setup a grunt task using something like grunt-sweet.js. You can then just install this module and require it when running sweet.js.
If you are totally new to macros, I recommend reading this excellent article:
Writing Your First Sweet.js Macro by James Long
Usage
The resolver
macro will match the following pattern:
resolver(FUNCITON, ARGS...)
And expand it to:
function(resolve, reject, notify) {
FUNCTION(ARGS..., function(err, result) {
if (err) {
reject(err);
}
resolve(result);
}
}
This allows you to wrap that resolver function in any promise library you want. It will take
any arguments in ARGS...
and pass them to FUNCTION
, and then add on the callback handling
function for you.