admin管理员组

文章数量:1336576

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

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

Share Improve this question asked Oct 7, 2017 at 20:52 hloughreyhloughrey 9784 gold badges11 silver badges23 bronze badges 6
  • saltPassword is a promise, so you need to use then on it to get the value via a callback. That is how async code works. You cannot expect to get a function return value that only arrives later when the function has already returned. NB: the second await is useless. – trincot Commented Oct 7, 2017 at 20:54
  • remove the await from the return statement. – Traveling Tech Guy Commented Oct 7, 2017 at 20:55
  • async functions always return promises. So you either need to call saltPassword from an async function and await it, or learn how to work with promises. – JLRishe Commented Oct 7, 2017 at 20:59
  • @trincot In this case, both awaits are useless because OP isn't doing anything with newHash other than returning it. – JLRishe Commented Oct 7, 2017 at 21:02
  • you can shorten the whole function to const saltPassword = async (password) => bcrypt.hash(password, saltRounds); But this doesn't change anything about the async nature of this task and that you have to deal with the promise – Thomas Commented Oct 7, 2017 at 21:04
 |  Show 1 more ment

2 Answers 2

Reset to default 2

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;

You need to return a Promise in order to use await. Simply wrap the callback function and call reject if there is an error or resolve if it was successful.

const saltPasswordAsync = (password, rounds) =>
    new Promise((resolve, reject) => {
      bcrypt.hash(password, rounds, (err, hash) => {
        if (err) reject(err);
        else resolve(hash)
      });
    });


async function doStuff() {
  try {
    const hash = await saltPasswordAsync('bacon', 8);
    console.log('The hash is ', hash);
  } catch (err) {
    console.error('There was an error ', err);
  }
}

doStuff();

Now you can use await to wait on the promise to resolve and use the value. To catch an error, wrap with a try/catch statement.

UPDATE

Thomas pointed out that you may not need to wrap the callback in a promise, since bcrypt returns a promise if you do not pass a callback function. You could replace the call to saltPasswordAsync above with bycript.hash like so:

const hash = await bcrypt.hash('bacon', 8);
console.log('The hash is ', hash);

本文标签: javascriptResolving Promise ltpendinggtStack Overflow