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

228
Visualizações
How do I fix function returning early?

I am trying to write a function to get data from a database, but the code in the function doesn't seem to be getting run from top to bottom. This is my code:

function getTextDb(id){
    db.get(id).then(function(doc){
        console.log("This is what should show: " + doc.text); //Just to show what should be returned
        return "This is what the return should show: " + doc.text;
    });
}

but then when I run console.log(getTextDb('randomText')) (This is a piece of data in the database) it shows this in the console (ignore the color, it's just stack overflow adding it):

undefined
This is what should show: Just some random text

Which means it's returning before getting to the actual return statement. What is going on?

about 4 years ago · Juan Pablo Isaza
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