title: Using Promises in Sequelize Hooks
body: <p>in the <a href="http://docs.sequelizejs.com/manual/tutorial/hooks.html#declaring-hooks" rel="nofollow noreferrer">Sequelize documentation I've read that you can use callbacks or return promises inside your hooks</a>. However, the following code fails to work for me – for both the latest 1.x and 2.x versions of Sequelize.</p>
  
  <p><strong>I setup the user model:</strong></p>
  
  <pre><code>var User = function(sequelize, DataTypes) {
      var User = sequelize.define('User', {
          firstName: DataTypes.STRING,
          lastName: DataTypes.STRING,
          email: DataTypes.STRING,
          password: DataTypes.STRING
      }, {
          hooks: {
              beforeUpdate: _hashPassword,
              beforeCreate: _hashPassword
          }
      });
  
      return User;
  };
  </code></pre>
  
  <p><strong>This is my function that hashes the user's password, using promises (doesn't work):</strong></p>
  
  <pre><code>function _hashPassword(user) {
      if (!user.getDataValue('password')) {
          return Promise.reject();
      }
  
      return bcryptGenSalt(10)
          .then(function(salt) {
              return bcryptHash(user.getDataValue('password'), salt);
          })
          .then(function(hash) {
              user.setDataValue('password', hash);
  
              return user;
          });
  }
  </code></pre>
  
  <p>I can't create users with the hooks setup like this. I don't get an error, but nothing happens  – no users are created / saved to the database. If I change my <code>_hashPassword(user)</code> method to call a callback function on success instead of returning the promise, the user is created and the password is successfully hashed:</p>
  
  <p><strong>This is my hash function that uses the callback (works):</strong></p>
  
  <pre><code>function _hashPassword(user, cb) {
      if (!user.getDataValue('password')) {
          return Promise.reject();
      }
  
      bcryptGenSalt(10)
          .then(function(salt) {
              return bcryptHash(user.getDataValue('password'), salt);
          })
          .then(function(hash) {
              user.setDataValue('password', hash);
  
              cb(null, user);
          });
  }
  </code></pre>
  
  <p>I would really like to use promises for this to keep things consistent. Am I missing something? Is this a bug or is there an error in the documentation? This behaviour is the same in the latest 1.x and 2.x versions of Sequelize for me.</p>
  
  <p>Thank you very much for your help! :)</p>