promiqueen
v1.0.0
Published
Promisify wrapper that yields automatic handling of Promise or node-style callback based on invocation parameters.
Downloads
3
Maintainers
Readme
promiqueen
A wrapper that detects if node-style callbacks are used. After the wrapper is applied, if a callback is supplied then it is used, but if a callback is missing (i.e. the last parameter is not a function), then a BlueBird promise is used.
Use with regular objects:
var promiqueen = require('promiqueen');
var context = {
method: promiqueen(function(arg,callback) {
callback(err,data);
})
};
// ...
context.method('crown',function(err,data) {
if ( err ) console.warn(err);
console.log('data',data);
});
context.method('crown').then(function(data) {
console.log('data',data);
}).catch(function(err) {
console.warn(err);
});
Use with classes:
var promiqueen = require('promiqueen');
var SomeClass = function() {};
SomeClass.prototype.method = promiqueen(function(arg,callback) {
callback(err,data);
});
// ...
var instance = new SomeClass();
instance.method('crown',function(err,data) {
if ( err ) console.warn(err);
console.log('data',data);
});
instance.method('crown').then(function(data) {
console.log('data',data);
}).catch(function(err) {
console.warn(err);
});