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

167
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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