Skip to content

Instantly share code, notes, and snippets.

@Elevista
Last active January 9, 2018 04:09
Show Gist options
  • Save Elevista/10eae60ea3dc0e690ce4aa95fe924e3a to your computer and use it in GitHub Desktop.
Save Elevista/10eae60ea3dc0e690ce4aa95fe924e3a to your computer and use it in GitHub Desktop.
seq(1)(fn1)(fn2)(fn3).value
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