pwrap
Add promise capability to any function.
Installation
npm install --save pwrap
Why?
Write
var myFunc = pwrap(function(resolve, reject) {
// do stuff
})
instead of writing
var myFunc = function() {
return new Promise(function(resolve, reject) {
// do stuff
})
}
and it only takes 800 bytes.
Usage
When you pwrap a function, you gain the ability to add resolve
and reject
as parameters, and the function will return a promise.
Important Note: pwrap requires you to bring your own promise implementation or have them natively.
// Use default promise or whatever Promise is set to
var pwrap = require('pwrap')();
// Use bluebird
var pwrap = require('pwrap')(require('bluebird'));
Real example:
var pwrap = require('pwrap')();
var timeoutPromise = pwrap(function(str, ms, resolve, reject) {
setTimeout(function() {
resolve(str);
}, ms);
});
// Logs after 1s
timeoutPromise('One second has passed!', 1000).then(function(result) {
console.log(result);
});
License
MIT