等待if语句和逻辑AND运算符

时间:2020-06-28 21:57:37

标签: javascript angular typescript ionic-framework async-await

  onAngularFireAuthChanged(): void {
    this.afAuth.authState.subscribe(async (user: firebase.User) => {

      if (user && !(await this.afAuth.currentUser)) {  // here is the place
        // code
      }
    });
  }

我可以使用await运算符来编写上述类型的&&吗?似乎对我不起作用。有任何线索吗?

this.afAuth.currentUser: (property) currentUser: Promise<firebase.User>

1 个答案:

答案 0 :(得分:1)

OP的反馈

是的,您是对的。我的代码工作正常。但是我的情况不正确。

原始

如有疑问,可以随时通过将await从条件if中移出来简化代码:

onAngularFireAuthChanged(): void {
    this.afAuth.authState.subscribe(async (user: firebase.User) => {
      const currentUser = await this.afAuth.currentUser;
      if (user && !currentUser) {  // here is the place
        // code
      }
    });
  }

我怀疑这是问题所在,您发布的代码应该可以正常工作。其他地方可能有问题。

相关问题