Last active
January 9, 2018 04:09
-
-
Save Elevista/10eae60ea3dc0e690ce4aa95fe924e3a to your computer and use it in GitHub Desktop.
seq(1)(fn1)(fn2)(fn3).value
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 _ = require('lodash') | |
function isPromise(result) { | |
return result && (typeof result.then === 'function') | |
} | |
function seq(init) { | |
let result = typeof init === 'function' ? init() : init | |
let properties = {value: {get() { return result }}} | |
_.forEach(['then', 'catch', 'finally'], x => { properties[x] = {get() { return (...args) => result[x](...args) }} }) | |
function chain() { | |
function tick(fn, ...args) { | |
let partial = _.partial(fn, ...args) | |
if (isPromise(result)) result = result.then(r => partial(r)) | |
else result = partial(result) | |
return chain() | |
} | |
Object.defineProperties(tick, properties) | |
return tick | |
} | |
return chain() | |
} | |
module.exports = module.exports.default = seq |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment