Skip to content

Instantly share code, notes, and snippets.

@pfeilbr
Last active May 20, 2016 17:00
Show Gist options
  • Save pfeilbr/7ab18eee039750cf45f979d0f464d20e to your computer and use it in GitHub Desktop.
Save pfeilbr/7ab18eee039750cf45f979d0f464d20e to your computer and use it in GitHub Desktop.
// see http://static-content-01.s3-website-us-east-1.amazonaws.com/Promise_promisifyAll___bluebird_1CEF7997.png
// catalist was to solve for promisifying cordova plugins and it's non-[standard|node] callback convention (successFn, errorFn) vs node's fn(error, success)
var Runner = function() {
return this;
};
Runner.prototype.echo = function(msg, success, failure){
success(msg);
};
Runner.prototype.echoFail = function(msg, success, failure){
failure(new Error('error!'))
};
Promise.promisifyAll(Runner.prototype, {
promisifier: function(cbApi) {
return function() {
var args = [].slice.call(arguments);
var self = this;
return new Promise(function(resolve, reject) {
args.push(resolve, reject);
cbApi.apply(self, args);
});
};
}
});
var r = new Runner()
r.echoAsync('hello')
.then(function(res) {
console.log(res) // called
})
.catch(function(err) {
console.error(err)
})
r.echoFailAsync('hello')
.then(function(res) {
console.log(res)
})
.catch(function(err) {
console.error(err) // called
})
null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment