Last active
December 31, 2015 01:49
-
-
Save micmarsh/7916978 to your computer and use it in GitHub Desktop.
Compose Promises and Synchronous Functions
This file contains 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
# 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