Skip to content

Instantly share code, notes, and snippets.

@raiscui
Forked from TheDeveloper/callback-to-promise.js
Created January 14, 2018 08:36
Show Gist options
  • Save raiscui/2bf2bd3d797b4633d3134e8c918aeb6e to your computer and use it in GitHub Desktop.
Save raiscui/2bf2bd3d797b4633d3134e8c918aeb6e to your computer and use it in GitHub Desktop.
A little function to convert a callback-style function to Promises
// usage: promisify(function([args..., cb]) { /* do stuff with args then cb */ })()
exports.promisify = function(fn) {
return function(...args) {
return new Promise((resolve, reject) => {
fn(...args, (err, result) => {
if (err) return reject(err);
resolve(result);
});
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment