Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Promisify all the functions in any object with a few lines of es6
const handler = {
get: function (target, prop, receiver) {
if (typeof target[prop] !== 'function' ) {
return target[prop]
}
return function () {
return new Promise((resolve, reject) => {
Reflect.get(target, prop, receiver).apply(target, [...arguments, function (err, result) {
if (err) {
return reject(err)
}
resolve(result)
}])
})
}
}
}
module.exports = function (thingToPromisify) {
return new Proxy(thingToPromisify, handler)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment