-
-
Save anodynos/4bceda9c34fad4e21063bde2884f191b to your computer and use it in GitHub Desktop.
using ES6 Proxy to deal with methods of Promise'd objects. not sure how useful this is yet.
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
// using ES6 Proxy to deal with methods of Promise'd objects. works for me in Edge though not Chrome somehow. | |
let handler = { | |
get: (target, prop) => function() { | |
if(target instanceof Promise) { | |
let args = arguments; | |
return target.then((o) => o[prop].apply(o, args)); | |
} else { | |
let value = target[prop]; | |
return typeof value == 'function' ? value.bind(target) : value; | |
} | |
} | |
}; | |
let obj = { greet: (name) => console.log('Hey ' + name) }; | |
let later = (v) => new Promise((resolve, reject) => setTimeout(() => resolve(v), 1000)) | |
let prom = later(obj); | |
let greeter = new Proxy(prom, handler); | |
greeter.greet('you'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment