Skip to content

Instantly share code, notes, and snippets.

@alexsasharegan
Last active August 21, 2017 16:16
Show Gist options
  • Save alexsasharegan/ba1db6b6e6346e56943c21c73c65316d to your computer and use it in GitHub Desktop.
Save alexsasharegan/ba1db6b6e6346e56943c21c73c65316d to your computer and use it in GitHub Desktop.
Promisify Node async functions dynamically. (curries when provided no additional arguments)
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