Skip to content

Instantly share code, notes, and snippets.

@astrotars
Last active July 26, 2018 20:29
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 astrotars/8698238fe689329cab7d67841681cbf5 to your computer and use it in GitHub Desktop.
Save astrotars/8698238fe689329cab7d67841681cbf5 to your computer and use it in GitHub Desktop.
// provided a string (password)
function validatePassword(password) {
// create promise with resolve and reject as params
return new Promise((resolve, reject) => {
// validate that password matches bambi (the deer)
if (password !== 'bambi') {
// password doesn't match, return an error with reject
return reject('Invalid Password!');
}
// password matches, return a success state with resolve
resolve();
});
}
function done(err) {
// if an err was passed, console out a message
if (err) {
console.log(err);
return; // stop execution
}
// console out a valid state
console.log('Password is valid!');
}
// dummy password
const password = 'foo';
// using a promise, call the validate password function
validatePassword(password)
.then(() => {
// it was successful
done(null);
})
.catch(err => {
// an error occurred, call the done function and pass the err message
done(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment