Last active
October 25, 2016 01:47
-
-
Save gunar/8301e1ceac86749b4a80 to your computer and use it in GitHub Desktop.
Handling empty promises
This file contains hidden or 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
// 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! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solvable by
Either.fromNullable
+eitherToTask