Skip to content

Instantly share code, notes, and snippets.

@gunar
Last active October 25, 2016 01:47
Show Gist options
  • Save gunar/8301e1ceac86749b4a80 to your computer and use it in GitHub Desktop.
Save gunar/8301e1ceac86749b4a80 to your computer and use it in GitHub Desktop.
Handling empty promises
// What is your opinion on the use of this function?
function rejectIfNil(msg) {
return payload => {
if (!payload) return Promise.reject(new Error(msg))
return payload
}
}
function update(_id) {
return User
.findOneAndUpdate({ _id }, { $set: { updated: true } }, { new: true })
.exec()
.then(rejectifNil(`Couldn't find user to update`))
.then(sendToView)
}
// Could this be improved? Thank you!
@gunar
Copy link
Author

gunar commented Oct 25, 2016

Solvable by Either.fromNullable + eitherToTask

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment