Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Promise proxy
let handlers = {}
handlers.get = function(target, property) {
let v = target[property]
if (typeof(v) == "function") {
return async (...args) => {
return new Promise((resolve, reject) => {
target[property](...args, (err, ...rest) => {
if (err) reject(err)
else resolve(rest.length == 1 ? rest[0] : rest)
})
})
}
} else {
return v
}
}
module.exports = function (obj) {
return new Proxy(obj, handlers)
}
let proxy = require('./promiseme')
let d = {}
d.x = 1
d.f = function (a, cb) {
let self = this;
setTimeout(function () { cb(null, self, a) }, 1000)
}
proxy(d).f("HELLO").then((x)=>{console.log(x)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.