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

131
Views
Destructuring async properties in JavaScript/TypeScript

Is there a way to get async properties with destructuring in JavaScript/TypeScript?

Like this:

class A {
  public get x() {
    return this.getX()
  }

  private async getX() {
    return 5
  }
}

async function f(a : A) {
  const { x } = a

  console.log(x) // Promise
}
async function g(a : A) {
  // works as const x = await a.x
  const { await x } = a

  console.log(x) // 5
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If using utility function is allowed, the following would work.
Its side effect is that the getters for all properties are executed.

const classToObject = async theClass => {
  const originalClass = theClass || {}
  const keys = Object.getOwnPropertyNames(Object.getPrototypeOf(originalClass))
  let classAsObj = {}
  for(const key of keys) {
    classAsObj[key] = await originalClass[key]    
  }
  return classAsObj
}


async function g(a : A) {
  // works as const x = await a.x
  const {x} = await classToObject(a);
  console.log(x) // 5
}
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!