我想在执行之前等待一个功能。我该怎么做
openOrganizationDialog(): void {
const dialogRef = this.dialog.open(NewOrganizationComponent, {
width: '800px',
height: '600px',
data: {
view: 'dialog'
}
});
dialogRef.afterClosed().subscribe(async result => {
// Want to wait on below line, so that my list gets updated
await this.getOrganizations();
let organizationPreSelectId = this.organizationList[0].orginizationId;
this.newLoanForm.patchValue({
orginizationId: organizationPreSelectId
});
});
}
这是我正在谈论的功能
async getOrganizations() {
await this.organizationService.getOrganizations().subscribe((response) => {
this.organizationList = response;
this.organizationList.sort((a, b) => (a.orginizationId < b.orginizationId) ? 1 : -1);
});
}
我已经尝试过async await
,但是它不起作用。
在此先感谢您的帮助。
答案 0 :(得分:1)
您将需要将可观察的值转换为承诺,以便等待。
async getOrganizations() {
this.organizationList = await this.organizationService.getOrganizations().toPromise();
this.organizationList.sort((a, b) => (a.orginizationId < b.orginizationId) ? 1 : -1);
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function