Skip to content

Instantly share code, notes, and snippets.

@joedotjs
Created August 8, 2016 15:43
Show Gist options
  • Save joedotjs/c4f88708251b3e4dfed96df18db8323f to your computer and use it in GitHub Desktop.
Save joedotjs/c4f88708251b3e4dfed96df18db8323f to your computer and use it in GitHub Desktop.
bluebird.promisify
Promise.promisify = function (fnThatTakesCallbacks) {
return function () {
var args = [].slice.call(arguments);
return new Promise(function (resolve, reject) {
fnThatTakesCallbacks.apply(this, args.concat(function (err) {
if (err) return reject(err);
var restOfArgs = [].slice.call(arguments, 1);
if (restOfArgs.length < 2) {
resolve(restOfArgs[0]);
} else {
resolve(restOfArgs);
}
}));
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment