Trying to hash a password using bcrypt inside an async function : <p>Following on from <a href="https://stackoverflow.com/questions/48799479/async-function-in-mongoose-pre-save-hook-not-working?noredirect=1#comment84602023_48799479">this question</a>.</p>

<p>I feel like I'm almost there, but my incomplete understanding of async is preventing me from solving this. I'm basically trying to just hash a password using bcrypt and have decided to seperate out the hashPassword function so that I can potentially use it in other parts of the app. </p>

<p><code>hashedPassword</code> keeps returning undefined though...</p>

<pre><code>userSchema.pre('save', async function (next) {

  let user = this
  const password = user.password;

  const hashedPassword = await hashPassword(user);
  user.password = hashedPassword

  next()

})

async function hashPassword (user) {

  const password = user.password
  const saltRounds = 10;

  const hashedPassword = await bcrypt.hash(password, saltRounds, function(err, hash) {

    if (err) {
      return err;
    }

    return hash

  });

  return hashedPassword

}
</code></pre>
