[英]In Angular 10 promise is not executing
In my project I'm using Angular 10, I have to call one API request async way.在我的项目中,我使用 Angular 10,我必须调用一个 API 请求异步方式。 So I tried with promises.所以我尝试了承诺。 but while executing the code it's not called the API. It always exists with the first line itself.但在执行代码时,它不叫 API。它始终存在于第一行本身。 I wasn't able to find the reason for this.我找不到原因。 It's not going for resolved or rejected.它不会解决或拒绝。
This is my ts.code:这是我的 ts.code:
public setUser(): () => Promise<any> {
return (): Promise<any> => {
return new Promise((resolve, reject) => {
//get user details
this.api.get<any>(environment.apiUserRoot + `/Users/current`).subscribe(
userData => {
this.storage.clearIdentity();
this.assignLoginUserToUser(userData.body);
},
err => {
this.storage.clearIdentity();
}
);
});
};
}
private assignLoginUserToUser(user: UserModel): void {
this.loginUser = user;
}
public getUserContext(): UserModel {
this.setUser();
return this.loginUser;
}
It's a bit too complex, I would rather simplify it by doing something like this:它有点太复杂了,我宁愿通过做这样的事情来简化它:
public setUser(): void {
this.api.get<any>(environment.apiUserRoot + `/Users/current`).subscribe({
next: (userData) => {
this.storage.clearIdentity();
this.assignLoginUserToUser(userData.body);
},
error: (err) => {
this.storage.clearIdentity();
},
});
}
It is not clear from the usage if you actually need a return value, but if you do, then you have a few options depending on the rxjs version that you use:如果您确实需要返回值,则用法并不清楚,但如果您需要,那么根据您使用的 rxjs 版本,您有几个选项:
firstValueFrom
:如果您没有firstValueFrom
:public setUser(): Promise<any> {
return this.api.get<any>(environment.apiUserRoot + `/Users/current`)
.toPromise().then(userData => {
this.storage.clearIdentity();
this.assignLoginUserToUser(userData.body);
return userData;
}).catch(() => {
this.storage.clearIdentity();
});
}
firstValueFrom
then you could do it like this:如果你有firstValueFrom
那么你可以这样做:public setUser(): Promise<any> {
return firstValueFrom(this.api.get<any>(environment.apiUserRoot + `/Users/current`))
.then(userData => {
this.storage.clearIdentity();
this.assignLoginUserToUser(userData.body);
return userData;
}).catch(() => {
this.storage.clearIdentity();
});
}
For getting the user, you have a couple of options depending on your async implementation choice (observable or promise; I personally recommend observables):为了获得用户,您有几个选项取决于您的异步实现选择(可观察或 promise;我个人推荐可观察):
setUser
should not perform the subscribe itself, and do the things it needs to do as a side effect using tap
and return the observable to be used later by the calling code.对于 observable, setUser
不应该自己执行订阅,而是使用tap
做它需要做的事情作为副作用,并返回 observable 以供稍后调用代码使用。public setUser(): Observable<UserModel> {
return this.api.get<any>(environment.apiUserRoot + `/Users/current`).pipe(
tap(result => {
this.storage.clearIdentity();
this.assignLoginUserToUser(result.body);
return result.body;
}),
catchError(err => {
this.storage.clearIdentity();
return of(null);
})
);
}
The getUserContext
should not be synchronous anymore, but return an Observable instead: getUserContext
不再是同步的,而是返回一个 Observable:
public getUserContext(): Observable<UserModel> {
return this.setUser();
}
This implies that you need to adjust the calling code to subscribe to this observable returned by the getUserContext
method like this:这意味着您需要调整调用代码以订阅getUserContext
方法返回的这个可观察对象,如下所示:
getUserContext().subscribe(user => {
// do something with the user here.
});
setUser
method using any of the implementations above (with .toPromise
or firstValueFrom
)有了承诺,您需要返回使用上述任何实现(使用.toPromise
或firstValueFrom
)从setUser
方法获得的 promisepublic getUserContext(): Promise<UserModel> {
return this.setUser().then(result => result.body);
}
The calling code in this case also needs some adjustments, in order to accommodate the fact that getUserContext
is not synchronous anymore:这种情况下的调用代码也需要一些调整,以适应getUserContext
不再同步的事实:
const user = await getUserContext();
or using then
或then
使用
getUserContext().then(user => {
// do something with the user here.
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.