I have two classes example1
and example2
. example1
is having async
method inside await keyword and promise response from API. When console inside async function am able to get print this.data
and this.data
but when I tried this to console inside example2
coming blank response where able to get response for this.key
. How to solve this issue. This might be due to resolving promising taking time. Any suggestion ? below is sample example.
One class :
class example1{
constructor(){
this.asyncFunction();
}
async asyncFunction(){
this.key = await this.callAPI(url);
this.data = await this.callAPI(url2);
}
callAPI(url) {
const metadata = new Promise((resolve) => {
fetch(url).then((res) => {
const json = res.json();
// console.log(json);
resolve(json);
});
});
return metadata;
}
}
Second class :
import example1; // just added to show it imported class
class example2{
constructor(){
const a = new example1();
console(a); // i got all response;
console(a.data) // got blank response;
}