Creating a user via social auth, merging and modifying appropriately 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 &amp;&amp; success(user);
  }, function(){
    error &amp;&amp; error();
  });
};

User.findByEmail = function(email, success, error){
  User.findOne({
    where: {
      email: email
    }
  }).then(function(user){
    success &amp;&amp; success(user);
  }, function(){
    error &amp;&amp; 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 &amp;&amp; success(user);
      }else{
        // if email address exists, merge user
        User.findByEmail(email, function(user){
          if(user){
            user.updateAttributes(kwargs).then(function(){ 
              success &amp;&amp; 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 &amp;&amp; success(user);
              }, function(){
                failure &amp;&amp; failure();
              });
            }, function(){
              failure &amp;&amp; failure();
            });
          }
        }, function(){
          failure &amp;&amp; failure();
        });
      }
  }, function(){
    failure &amp;&amp; failure();
  });
}
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 &amp;&amp; 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).