Skip to content

Instantly share code, notes, and snippets.

@anodynos
Forked from KiaraGrouwstra/proxy-promise.js
Created March 25, 2020 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anodynos/4bceda9c34fad4e21063bde2884f191b to your computer and use it in GitHub Desktop.
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.
// 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