Search code examples
javascriptasync-awaites6-promise

Resolving Promise <pending>


I'm looking to create a simple helper function which returns the hash for a given password using bcrypt but everytime i call the function, it resolves to Promises { <pending> } what am i doing wrong?

const saltPassword = async (password) => {
    const newHash = await bcrypt.hash(password, saltRounds, (err, hash) => {
        if (err) return err;
        return hash;
    });
    return await newHash;
}

cheers


Solution

  • You should do something like this

    const saltPassword = async (password) => {
      const newHash = await bcrypt.hash(password, saltRounds, (err, hash) => {
        if (err) return err;
        return hash;
      });
      return newHash; // no need to await here
    }
    
    // Usage
    const pwd = await saltPassword;