Last active
June 3, 2016 15:07
-
-
Save rstacruz/6c562c645878f62ccd3b31ea93cfbf31 to your computer and use it in GitHub Desktop.
Conditional promises
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
getArticles() | |
.then(condition({ | |
if: (data) => data.length > 10, | |
then: p => p | |
.then(paginateArticles), | |
else: p => p | |
.then(sortArticles) | |
}) |
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
function condition (spec) { | |
return function (value) { | |
if (spec.if(value)) { | |
return spec.then ? spec.then(Promise.resolve(value)) : value | |
} else { | |
return spec.else ? spec.else(Promise.resolve(value)) : value | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment