Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda