Skip to content

Instantly share code, notes, and snippets.

@matthieuprat
Last active September 12, 2015 21:13
Show Gist options
  • Save matthieuprat/ae6bfdce2a7b17c74179 to your computer and use it in GitHub Desktop.
Save matthieuprat/ae6bfdce2a7b17c74179 to your computer and use it in GitHub Desktop.
function promisify (func) {
return (...args) =>
new Promise((resolve, reject) => {
func(...args, function (err, ...args) {
if (err === null) {
resolve(args)
} else {
reject(err)
}
})
})
}
function foo (a, b, cb) { cb(null, a, b) }
function bar (cb) { cb('err') }
foo('a', 'b', console.log.bind(console))
// null "a" "b"
bar(console.log.bind(console))
// err
promisify(foo)('a', 'b').then(console.log.bind(console))
// ["a", "b"]
promisify(bar)().then(null, console.log.bind(console))
// err
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment