I have to wait for data from an API to set value of a variable and use it in a if condition.
The problem is i'm not clear how to wait for the data using async and await. I tried a few things below is the code .
currentObj.isObjEditable = this.isObjEditable(obj);
async isObjEditable(obj: any) {
if (this.isContentEditable === undefined) {
let test = await this.getsocialMediaData();
console.log(test);
}
else {
if (this.isContentEditable) {
if (some Condition1 )
return true;
else if (condition2)
return true;
else if (condition 3)
return true;
else {
return false;
}
}
else
return false;
}
async getsocialMediaData() {
this.commonService.getSocialMediaSettings().subscribe(value=> {
return value;
});
}
getSocialMediaSettings(){
return Observable.timer(0, 60000)
.switchMap(() => this.http.get(url, { headers: headers, method: 'GET' }))
.map(res => res.json());
}
when i log test it returns a promise.The value of isContentEditable is set from the data received from the API.
I know what i have done is not correct but i also dont know how to correct this or what is the right approach.
Please Guide
Thanks