I have a large script which performs a load of Promises and Async calls so I created a new class which I started moving promise functions into. But i am now struggling to have the class properties initialize.
class MyClass {
constructor(id) {
this._id = id
this._thing = []
}
getThing {
return this['_thing']
}
async init() {
this._thing = this.requestThing(this['_id'])
}
requestThing(id) {
return new Promise((resolve) => {
// Make request using id
resolve(response);
})
}
}
So I was thinking of something like this
const init = async () => {
let myClass = await new MyClass(123)
await myClass.intialise()
let test = myClass.getThing()
}
init()
But I seem to be getting errors. At this point my code is pretty hard to debug (or rewrite to make a simple test - hence the refactor) as I am using lots of Promises and async functions.
Is this the correct way to initialize a class which needs to make async calls to set its properties?