Created
August 27, 2018 17:46
-
-
Save leggsimon/8f769677fddb00edbf211194bdcea205 to your computer and use it in GitHub Desktop.
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
const post = (userId) => { | |
// First check the user exists | |
return getUser(userId) | |
.then(user => { | |
// Ok, so the user aleady exists | |
// do some logic | |
// Lets try and delete them | |
return deleteUser(userId) | |
.then(() => { | |
// happy days, return a 204 | |
return 204; | |
}) | |
.catch(() => { | |
// Something went wrong, return 500 | |
return 500; | |
}); | |
}) | |
.catch(error => { | |
// User doesn’t exist, so return a 404 | |
return 404; | |
}); | |
} | |
/* | |
So on line 7, if that logic threw an error, it would be caught | |
by the catch block returning the 404, which would be misleading. | |
In that case, I might want to move that `.catch` block to immediately | |
after the call to getUser. But If I do that, by *returning* the `404`, | |
that then gets passed into the `.then` block which calls deleteUser | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why does
getUser
throw if no matching user is found? Is it your own function or someone else's? Returning 404 on catch seems incorrect since you are not checking what caused the throw.