Skip to content

Instantly share code, notes, and snippets.

@JustusAdam
Created October 2, 2015 09:49
Show Gist options
  • Save JustusAdam/7698ac41f075a41510bd to your computer and use it in GitHub Desktop.
Save JustusAdam/7698ac41f075a41510bd to your computer and use it in GitHub Desktop.
-- User.signup = function(user) {
-- return new Promise(function(resolve, reject) {
-- User.validate(user).then(function() {
-- return Bcrypt.genSalt(10);
-- }).then(function(salt) {
-- return Bcrypt.hash(user.password, salt, null);
-- }).then(function(saltedHashedPassword) {
-- return User.insertIntoDatabase(user, saltedHashedPassword);
-- }).then(function(userRecord) {
-- resolve(userRecord);
-- }).catch(function(error) {
-- reject(error);
-- });
-- });
-- };
type Promise a = Either String a
-- in normal bind form
signup :: User -> Promise ()
signup user =
validate >>
Bcrypt.genSalt 10 >>=
\salt -> Bcrypt.hash (user^.password) salt Nothing >>=
User.insertIntoDatabase user
-- in 'do' notation form
signup :: User -> Promise ()
signup user = do
validate
salt <- Bcrypt.genSalt 10
saltedHashedPassword <- Bcrypt.hash (user^.password) salt Nothing
userRecord <- User.insertIntoDatabase user saltedHashedPassword
return userRecord
-- mixed style (how I would write it)
signup :: User -> Promise ()
signup user = do
validate
salt <- Bcrypt.genSalt 10
Bcrypt.hash (user^.password) salt Nothing >>= User.insertIntoDatabase user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment