Skip to content

Instantly share code, notes, and snippets.

@malko
Created March 8, 2017 13:33
Show Gist options
  • Save malko/d3dee36e7d0d927654439f4c01c8ca0c to your computer and use it in GitHub Desktop.
Save malko/d3dee36e7d0d927654439f4c01c8ca0c to your computer and use it in GitHub Desktop.
add a fluent interface on object with promise methods
const enchainProxifier = (target, promise = Promise.resolve()) => {
return new Proxy(target, {
get(target, propName) {
if (propName === 'promise') {
return promise;
} else if (propName === 'then') {
return (...args) => promise.then(...args);
}
if (target[propName] instanceof Function) {
return (...args) => enchainProxifier(target, promise.then(() => target[propName](...args)));
}
return target[propName];
}
});
};
module.exports = enchainProxifier;
@arturz
Copy link

arturz commented Sep 28, 2017

Could you show some example?

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