Skip to content

Instantly share code, notes, and snippets.

@micmarsh
Last active December 31, 2015 01:49
Show Gist options
  • Save micmarsh/7916978 to your computer and use it in GitHub Desktop.
Save micmarsh/7916978 to your computer and use it in GitHub Desktop.
Compose Promises and Synchronous Functions
# use lodash/underscore, this could probably actually be a mixin for those
compose = (fns) ->
_.reduceRight fns, (composed, currentFn) ->
_.compose (valueOrPromise) ->
vop = valueOrPromise
if Boolean vop and vop.then
vop.then (value) ->
currentFn value
else
currentFn vop
, composed
###
non-working version that would have no dependencies
compose = (fns) ->
fns.reverse().reduce (composed, currentFn) ->
(valueOrPromise) ->
vop = valueOrPromise
if Boolean vop and vop.then
vop.then (value) ->
currentFn composed value
else
currentFn composed vop
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment