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

103
Views
how to return in connection.query

I have a problem... I wanted to return "A" but The result was "B" I don't know what to do Please help me

my code:

async function getID(){

    let sql = 'SELECT * from id'

    connection.query(sql, (err, rows) => {
        if (err)
            throw err;

        console.log(rows)
        return "A"
        // return rows

    })
    return "B"
}

let list = getCredit();
list.then(console.log);
console.log(list)

My results

    [2022/06/13 21:34:29.548] Promise { 'B' }
    [2022/06/13 21:34:29.549] B
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You are mixing here callback with promises. To reach what you want, wrap your function in a promise:

function getID(){
  return new Promise((resolve, reject) => {

    let sql = 'SELECT * from id'

    connection.query(sql, (err, rows) => {
        if (err){
           reject(err)
        }else{
           resolve(rows);
        }
    })

  })
}

let list = getCredit();
list.then(console.log);
console.log(list)
about 4 years ago · Santiago Gelvez Report
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!