Skip to content

Instantly share code, notes, and snippets.

@rstacruz
Last active June 3, 2016 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rstacruz/6c562c645878f62ccd3b31ea93cfbf31 to your computer and use it in GitHub Desktop.
Save rstacruz/6c562c645878f62ccd3b31ea93cfbf31 to your computer and use it in GitHub Desktop.
Conditional promises
getArticles()
.then(condition({
if: (data) => data.length > 10,
then: p => p
.then(paginateArticles),
else: p => p
.then(sortArticles)
})
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