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

227
Vistas
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 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