Created
April 7, 2014 23:07
-
-
Save ithinkihaveacat/10071797 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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