Skip to content

Instantly share code, notes, and snippets.

@wraithgar
Created May 24, 2020 19:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wraithgar/15b33dee641fb3b0265eafe34269a093 to your computer and use it in GitHub Desktop.
Save wraithgar/15b33dee641fb3b0265eafe34269a093 to your computer and use it in GitHub Desktop.
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)
}
@wraithgar
Copy link
Author

This has been turned into a real module with a little more complete feature set https://github.com/wraithgar/gar-promisify

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment