Skip to content

Instantly share code, notes, and snippets.

@KiaraGrouwstra
Created August 21, 2016 16:00
Show Gist options
  • Save KiaraGrouwstra/ac9c024e0996fda7c4d1290241e26277 to your computer and use it in GitHub Desktop.
Save KiaraGrouwstra/ac9c024e0996fda7c4d1290241e26277 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.
// 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');
@anodynos
Copy link

@bekharsky thanks - I know my code had caused us some issues back then (so we backtracked from it), please do let us know if you have encountered any issues and please post us any updated code if you fix them ;-)

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