2
\$\begingroup\$

I'm using Sequelise with node.js, and I have a feeling the paradigm I'm using to modify objects is not working well, as my code is getting messy rather quickly.

The function User.createOrMerge is designed to be called after a successful social auth (Google, Facebook etc.), with kwargs being the social auth token to check against the user (e.g. { googleId: "some-oauth-token" }).

I seem to be writing a lot of code to deal with the overhead of using promises. Is there a way to do this more elegantly or is this my life now?

User.findByUsername = function(username, success, error){
  User.findOne({
    where: {
      username: username
    }
  }).then(function(user){
    success && success(user);
  }, function(){
    error && error();
  });
};

User.findByEmail = function(email, success, error){
  User.findOne({
    where: {
      email: email
    }
  }).then(function(user){
    success && success(user);
  }, function(){
    error && error()
  });
}

User.createOrMerge = function(username, email, kwargs, success, failure){
  // if user with kwargs exists, return that
  User.findOne({
    where: kwargs
  }).then(function (user) {
      if(user){
        success && success(user);
      }else{
        // if email address exists, merge user
        User.findByEmail(email, function(user){
          if(user){
            user.updateAttributes(kwargs).then(function(){ 
              success && success(user);
            });
          }else{
            // if username exists, modify this one
            User.findByUsername(username, function(user){
              if(user){
                username += UUID();
              }
              // finally create a new user
              kwargs['username'] = username;
              kwargs['email'] = email;
              User.create(kwargs).then(function(user){
                success && success(user);
              }, function(){
                failure && failure();
              });
            }, function(){
              failure && failure();
            });
          }
        }, function(){
          failure && failure();
        });
      }
  }, function(){
    failure && failure();
  });
}
\$\endgroup\$
1
\$\begingroup\$

You can organize better your asynchronous code.

Turn something like this:

Promisse.then(function() {
    Promisse.then(function() {
        promisse.then(function()) {
        }, function() { error })
    }, function() { error })
}, function() { error })

Into something like:

Promisse.then(function() {
}).then(function() {
}).then(function(){
}).catch(function () { failure && failure(); })

Another tip would be creating a function User.create and other called User.merge (this will make debugging easier; read about the Single-responsiblity principle to know more in depth why).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.