Skip to content

Instantly share code, notes, and snippets.

@colbydauph
Last active March 21, 2018 20:45
Show Gist options
  • Save colbydauph/b3a1aaddd4638e411c82ce0336609826 to your computer and use it in GitHub Desktop.
Save colbydauph/b3a1aaddd4638e411c82ce0336609826 to your computer and use it in GitHub Desktop.
Promisify + Callbackify
// inverse of callbackify
exports.promisify = (func) => (...args) => {
return new Promise((resolve, reject) => {
func(...args, (err, data) => {
err ? reject(err) : resolve(data);
});
});
};
// inverse of promisify
exports.callbackify = (func) => (...args) => {
const cb = args.pop();
Promise
.resolve(args)
.then((args) => func(...args))
.then((res) => cb(null, res))
.catch((err) => cb(err));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment