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

138
Views
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
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!