Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

630
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda