[英]Stop executing code until my async/await HttpClient get operation finishes executing code (including html calls)
I need to execute HttpClient
get request synchronously so I found a solution on internet to use toPromise
instead of subscribe and then await.我需要同步执行
HttpClient
获取请求,所以我在互联网上找到了使用toPromise
而不是 subscribe 然后等待的解决方案。 However I have noticed that the this.form = = this.formBuilder.group
line of code executes before myAccount
is initialized by the async
call.但是我注意到
this.form = = this.formBuilder.group
代码行在myAccount
被async
调用初始化之前执行。 In other words I was expecting that the execution code will block until this.myAccount
is initialized and then starts executing this.form = this.formBuilder.group
line.换句话说,我期望执行代码会阻塞,直到
this.myAccount
被初始化,然后开始执行this.form = this.formBuilder.group
行。 Am I understanding async/await in javascript correct.我是否理解 javascript 中的 async/await 正确。 What do I need to do to achieve what I want - that is, wait with execution until
this.myAccount
is initialized by the call await this.accountService.getById(this.id).toPromise()
.我需要做什么才能实现我想要的 - 也就是说,等待执行直到
this.myAccount
被调用await this.accountService.getById(this.id).toPromise()
初始化。
ngOnInit(): void {
this.getByIdSynchronously();
this.form = this.formBuilder.group({
scheduledDate: ['', Validators.required],
required: [''],
userAvailability: [''],
});
}
async getByIdSynchronously()
{
this.myAccount = await this.accountService.getById(this.id).toPromise();
}
I also have an html page with the first line of code looking like this:我还有一个 html 页面,第一行代码如下所示:
<form [formGroup]="form" (ngSubmit)="onSubmit()" *ngIf="!isAdmin">
...
</form >
where isAdmin is a local function:其中 isAdmin 是本地 function:
get isAdmin()
{
if(this.myAccount)
{
return this.myAccount.role == Role.Admin;
}
else{
return true;
}
}
which executes dozen of time before my async/await method finishes so I have to return some artificial true value until my this.myAccount
is initialized.它在我的 async/await 方法完成之前执行了几十次,所以我必须返回一些人为的真实值,直到我的
this.myAccount
被初始化。
Note笔记
I have found solution to the first problem - just use then
operator of the Promise.我找到了第一个问题的解决方案 - 只需使用 Promise 的
then
运算符。 I can't find the solution for the second problem - to stop isAdmin
being called from template html until this.myAccount
is initialized.我找不到第二个问题的解决方案 - 停止从模板
isAdmin
调用 isAdmin 直到this.myAccount
被初始化。 Is there a remedy for that?有补救措施吗?
Instead of Promises you should use Observables as Angular also encourages the use of Observables.你应该使用 Observables 而不是 Promises,因为 Angular 也鼓励使用 Observables。 The HTTPClient returns an observable so you can subscribe to it instead.
HTTPClient 返回一个 observable,因此您可以订阅它。
AccountService账户服务
getById(id: number): Observable<any> {
return this.httpClient.get(`URL goes here. ID goes into PARAMS`);
}
Then in your component subscribe to your API call.然后在您的组件中订阅您的 API 调用。 You can use another variable to stop isAdmin from being called continuously.
您可以使用另一个变量来阻止 isAdmin 被连续调用。
isLoaded: boolean = false;
ngOnInit(): void {
this.accountService.getById(this.id).subscribe(resp=>{
this.form = this.formBuilder.group({
scheduledDate: ['', Validators.required],
required: [''],
userAvailability: [''] });
this.isLoaded = true;
});
}
<form [formGroup]="form" (ngSubmit)="onSubmit()" *ngIf="isLoaded && !isAdmin">
...
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.