Skip to content

Instantly share code, notes, and snippets.

@nchanged
Last active July 26, 2016 07:43
Show Gist options
  • Save nchanged/c7b079b82e25c86bf5001b079c3875b3 to your computer and use it in GitHub Desktop.
Save nchanged/c7b079b82e25c86bf5001b079c3875b3 to your computer and use it in GitHub Desktop.
Registration Flow (promises)
let pwd, email, user;
UserFlow.validateEmail(email)
.then(UserFlow.checkUserExists) // Check if user exists
.then(function(existingUser){ // Spitting custom exception
if( existingUser ){
throw Error('User exists')
}
}).then(() => {
return UserFlow.hashPassword(pwd); // Hash password
}).then((hashedPwd)=>{
return UserFlow.createUser(email, hashedPwd); // Actually creating user in the databse
}).then((newUser) => {
user = newUser // Saving user to later reference
return sendEmail(user, pwd) // Sending an email to the user
}).then(() => {
return user // Returning the user object (model)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment