Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

629
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda