Skip to content

Instantly share code, notes, and snippets.

@SeanJM
Last active October 31, 2017 20:50
Show Gist options
  • Save SeanJM/a9cb0399c5fcd4fb2de711715a3dc3da to your computer and use it in GitHub Desktop.
Save SeanJM/a9cb0399c5fcd4fb2de711715a3dc3da to your computer and use it in GitHub Desktop.
A function which will turn node style callbacks into Promises.
module.exports = function promisify(fn) {
const args = [];
for (var i = 1, n = arguments.length; i < n; i++) {
args.push(arguments[i]);
}
return new Promise(function (resolve, reject) {
args.push(function (err) {
const args = [];
if (err) {
reject(err);
} else {
for (var i = 1, n = arguments.length; i < n; i++) {
args.push(arguments[i]);
}
resolve.apply(resolve, args);
}
});
fn.apply(fn, args);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment