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

232
Views
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
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!