-
-
Save navanathjadhav/a05c2cec45cad4f0c194941807e840c7 to your computer and use it in GitHub Desktop.
Example of guard clause
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
/* | |
* Here important code is written inside long if block | |
*/ | |
function login(username, password) { | |
const user = await UserSchema.find({ email: username, password: password }) | |
if (user) { | |
// Important code... | |
} else { | |
return new Error('Invalid credentials') | |
} | |
} | |
/* | |
* Here user is asserted at the beginning and the error has been returned thus stopping the further unnecessary execution | |
*/ | |
function login(username, password) { | |
const user = await UserSchema.find({ email: username, password: password }) | |
/* | |
* Guard Clause | |
*/ | |
if (!user) { | |
return new Error('Invalid credentials') | |
} | |
// Important code... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment