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

164
Views
Returning an unresolved promise (async/await) in javascript

I was hoping someone could help me make sense of why the following code isn't working properly.

In the Expense Data class, I have a function listExpenses which should only return data once the promise is either resolved or rejected.

However, when I instantiate a new expenseData object and then try to log a list of expenses, I get a pending Promise. I would have expected the Promise to always be resolved at this stage.

class ExpenseData {
  constructor() {
    this.client = new Client({ database: 'expenses' })
  }

  async listExpenses() {
    await this.client.connect().catch(err => logAndExit(err));
    let data = await this.client.query("SELECT * FROM expenses ORDER BY created_on ASC")
                                .catch(err => logAndExit(err))
    return data;
  }
}
let expenseData = new ExpenseData(); 
let myData = expenseData.listExpenses();
console.log(myData);

Here's what I get in my console after running this code: Promise { <pending> }

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your method is asynchronous meaning it immediately returns a Promise that will be resolved or rejected depending on what will happen in the method's body.

You need to either do this:

const func = async () => {
  let expenseData = new ExpenseData(); 
  let myData = await expenseData.listExpenses();
  console.log(myData);
};

func();

Or this:

let expenseData = new ExpenseData(); 
expenseData.listExpenses().then((myData) => console.log(myData));
about 4 years ago · Juan Pablo Isaza 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!