Skip to content

Instantly share code, notes, and snippets.

@ithinkihaveacat
Created April 7, 2014 23:07
Show Gist options
  • Select an option

  • Save ithinkihaveacat/10071797 to your computer and use it in GitHub Desktop.

Select an option

Save ithinkihaveacat/10071797 to your computer and use it in GitHub Desktop.
function F(fn) {
return function (/* arguments */) {
var args = [this].concat(Array.prototype.slice.call(arguments));
return new Promise(function (resolve, reject) {
Promise.all(args).then(function (/* arguments */) {
try {
Promise.resolve(
fn.apply(arguments[0].shift(), arguments[0])
).then(resolve, reject);
} catch (e) {
reject(e);
}
}, reject);
});
}
}
function C(obj) {
function Proxy() { }
// Synthesise a "get" method for property access
Proxy.prototype.get = F(function (p) {
return this[p];
}).bind(obj);
for (m in obj.constructor.prototype) {
Proxy.prototype[m] = F(obj.constructor.prototype[m]).bind(obj);
}
return new Proxy();
}
function Number(i) {
this.i = i;
}
Number.prototype.add = function (j) {
return this.i + j;
}
Number.prototype.sub = function (j) {
return this.i - j;
}
var log = console.log.bind(console);
var n = new Number(8);
var p = C(n);
p.add(3).then(log);
p.add(5).then(log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment