【发布时间】:2018-11-09 06:21:01
【问题描述】:
我必须等待来自 API 的数据来设置变量的值并在 if 条件下使用它。
问题是我不清楚如何使用异步和等待来等待数据。 我试了几件事,下面是代码。
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());
}
当我记录测试时,它返回一个承诺。isContentEditable 的值是根据从 API 接收到的数据设置的。
我知道我所做的不正确,但我也不知道如何纠正或正确的方法是什么。
请指导
谢谢
【问题讨论】:
标签: angular typescript async-await