Created
March 8, 2017 13:33
-
-
Save malko/d3dee36e7d0d927654439f4c01c8ca0c to your computer and use it in GitHub Desktop.
add a fluent interface on object with promise methods
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
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you show some example?