I need to write a class that fetch data on creation and then use that data asynchronously. nut not sure how I can use a class that handles data asynchronously in JavaScript?
I need to write a class that fetch data on creation and then use that data asynchronously. something like this. but this code throws error as the instance is not promise
class Features {
constructor(){
this.features = null;
this.fetchFeatures();
}
async fetchFeatures () {
const result = await fetchAllFeatures();
this.features = result;
}
}
const featuresInstance = new Features();
featuresInstance.then((res) => console.log(featuresInstance.features));