【发布时间】:2019-05-08 10:11:49
【问题描述】:
您好,我使用 Angular 6 使用以下代码调用 rest api。我正在尝试使代码与 async-await 函数同步。但是缺少一些东西
async save() {
if (this.changedRecords.length !== 0) {
this.post('/api/devices/update-devices', this.changedRecords).
then(x => { console.log("change"); console.log(`Resolved: ${x}`) });
}
if (this.newRecords.length !== 0) {
this.post('/api/devices/new-devices', this.newRecords).
then(x => { console.log("new"); console.log(`Resolved: ${x}`) });
}
if (this.deletedRecords != null) {
this.post('/api/devices/delete-devices', this.deletedRecords).
then(x => { console.log("deleted"); console.log(`Resolved: ${x}`) });
}
}
async post(url: string, list: DboDevice[]) {
var result;
if (list.length !== 0) {
await this.http.post(url, list).subscribe(result => {
result = true;
}, error => {
console.error(error);
result = false;
});
}
else {
result = true;
}
return result;
}
但是,当我运行此代码时,这些值在控制台中返回为“已解决:未定义”。这让我相信 await 不会停止 post() 函数中的程序。我在这里做错了什么?
【问题讨论】:
标签: angular async-await rxjs angular6 angular-promise