【发布时间】:2025-12-05 04:25:01
【问题描述】:
我想在执行继续之前等待一个函数。我该怎么做
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,但它不起作用。
在此先感谢您的帮助。
【问题讨论】:
-
等待订阅不会做任何事情;要么返回一个 observable,要么将其转换为 Promise 并处理它。
标签: angular asynchronous async-await