Javascript 使用if语句和逻辑and运算符等待
我可以用Javascript 使用if语句和逻辑and运算符等待,javascript,angular,typescript,ionic-framework,async-await,Javascript,Angular,Typescript,Ionic Framework,Async Await,我可以用&&操作符编写上述类型的wait吗?这似乎对我不起作用。有线索吗 this.afAuth.currentUser:(属性)currentUser:PromiseOP的反馈 是的,你说得对。我的代码运行良好。但我的情况并不正确 原创 当有疑问时,您可以通过将wait移出if条件框来简化代码: onAngularFireAuthChanged(): void { this.afAuth.authState.subscribe(async (user: firebase.User)
&&
操作符编写上述类型的wait
吗?这似乎对我不起作用。有线索吗
this.afAuth.currentUser:
(属性)currentUser:Promise
OP的反馈
是的,你说得对。我的代码运行良好。但我的情况并不正确
原创
当有疑问时,您可以通过将wait
移出if
条件框来简化代码:
onAngularFireAuthChanged(): void {
this.afAuth.authState.subscribe(async (user: firebase.User) => {
if (user && !(await this.afAuth.currentUser)) { // here is the place
// code
}
});
}
我怀疑这是问题所在,但是你发布的代码应该可以正常工作。其他地方可能有问题。为什么不使用变量作为
的结果等待这个。afAuth.currentUser
?@HereticMonkey是的,我可以这样做。但是如果我能像上面那样做的话,我想减少alet/const
。你在这里混合了一些隐喻。你有在rxjs中使用的subscribe
,还有async
/wait
,这是承诺的领域。将两者混用通常是个坏主意(tm)。我认为您应该使AngularFireAuthChanged异步
并使用const user=wait this.afAuth.authState.toPromise()
如果您想继续使用异步
/等待
,或者在任何地方都使用rxjs。“它似乎对我不起作用”不是一个足够精确的错误描述来帮助您。什么不起作用?它怎么不起作用?你的代码有什么问题?你收到错误信息了吗?错误消息是什么?你得到的结果不是你期望的结果吗?你期望得到什么样的结果?为什么?你会得到什么样的结果?两者有什么不同?你观察到的行为是否不是期望的行为?期望的行为是什么?为什么?观察到的行为是什么?它们有什么不同?“看起来”是什么意思?它到底行不行?
onAngularFireAuthChanged(): void {
this.afAuth.authState.subscribe(async (user: firebase.User) => {
const currentUser = await this.afAuth.currentUser;
if (user && !currentUser) { // here is the place
// code
}
});
}