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

140
Vistas
how to access data outside promise .then

so I have class for data object, I'm trying to define one property where I fetch it's value from api

this is how the class looks like:

export default class User extends Base {
  ....
  constructor(packet: Packet) {
    ....
    this.card = this.get_user_card() // showing undefined
  }

  get_user_card(): Card {
    this.rest.api(...).get().then((res: any) => {
      console.log(res.data); //  return the card data
      return res.data;
    })
  }
}

it's showing undefined when I set to card properties outside the function

I even tried this but this one showing Promise <pending> I can't use await inside constructor

return this.rest.api(...).get().then((res: any) => {
  return res.data;
})

and this but it's showing undefined for both and if I make it asynchronous it will return promise pending for the this.card

const res = this.rest.api(...).get()
console.log(res.data) //undefined
return res.data; //undefined too

tried this one too and doesn't work

this.rest.api(...).get().then((res: any) => {
  this.channel = res.data; //not working
  // even assign
  Object.assign(this, { card: res.data }); //not either
})

but console logging inside their .then is working. can someone help me or have other solution?

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