Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

631
Views
With async the bcrypt.hash function returns undefined but it works fine with .then

Here is the code with async (returns undefined)

userService.register = (username, password) => {
  return bcrypt.hash(password, saltRounds, async(err, hash) => {
    const newUser = new user({
      username: username,
      password: hash
    })
    return await newUser.save()
  })
}

and this is the same code with .then, it works correctly

userService.register = (username, password) => {
  return bcrypt.hash(password, saltRounds)
    .then(hash => {
      const newUser = new user({
        username: username,
        password: hash
      })
      return newUser.save()
    })
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This is documented behaviour:

Async methods that accept a callback, return a Promise when callback is not specified if Promise support is available

You're passing a callback in your first example, so bcrypt doesn't return a promise.

Another way to write your code is to make the wrapping function async:

userService.register = async (username, password) => {
  const hash    = await bcrypt.hash(password, saltRounds);
  const newUser = new user({
    username: username,
    password: hash
  })
  return await newUser.save(); // or just `return newUser.save()`
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!