Last active
March 21, 2018 20:45
-
-
Save colbydauph/b3a1aaddd4638e411c82ce0336609826 to your computer and use it in GitHub Desktop.
Promisify + Callbackify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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