Last active
August 21, 2017 16:16
-
-
Save alexsasharegan/ba1db6b6e6346e56943c21c73c65316d to your computer and use it in GitHub Desktop.
Promisify Node async functions dynamically. (curries when provided no additional arguments)
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
function promisify(func, ...args) { | |
if (arguments.length === 1) { | |
// Curry with the decremented arity | |
return promisify.bind(null, func) | |
} | |
return new Promise((resolve, reject) => { | |
func(...args, (error, ...cbArgs) => { | |
if (error) { | |
return reject(error) | |
} | |
resolve(cbArgs) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment