Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我如何等待订阅_Javascript_Angular_Typescript_Angular6 - Fatal编程技术网

Javascript 我如何等待订阅

Javascript 我如何等待订阅,javascript,angular,typescript,angular6,Javascript,Angular,Typescript,Angular6,我使用了下面的代码,直到我发现getConnectedUser()函数比verifyUser()花费的时间更长,所以this.userUID是未定义的: this.layoutService.getConnectedUser().subscribe( (data) => { this.userSaml = data; this.layoutService.connectedUser.matricule = this.userSaml.matricule; th

我使用了下面的代码,直到我发现getConnectedUser()函数比verifyUser()花费的时间更长,所以this.userUID是未定义的:

this.layoutService.getConnectedUser().subscribe(

  (data) => {
    this.userSaml = data;
    this.layoutService.connectedUser.matricule = this.userSaml.matricule;
    this.layoutService.connectedUser.profil = this.userSaml.profil;
    this.layoutService.connectedUser.uid = this.userSaml.uid;
    this.layoutService.connectedUser.username = this.userSaml.username;
    this.layoutService.connectedUser.city = this.userSaml.city;
    console.log("dashboard this.layoutService.connectedUser", this.layoutService.connectedUser);
  },
  (err) => {
    throw err;
  }
);

      this.userUID = this.layoutService.connectedUser.uid;
      console.log("this.userUID", this.userUID);
      this.adminService.verifyUser(this.userUID).subscribe(
        (data) => {
          this.userStatus = data[0].status;
          this.userProfile = data[0].profil;
          console.log("userProfile" + JSON.stringify(data[0].profil));
          this.userExists = true;
        },
        (err) => {
          this.userExists = false;
        }
      );
因此,我想确保getConnectedUser subscribe已完成以调用第二个,我更改了代码并添加了.add方法,如下所示:

此.layoutService.getConnectedUser().subscribe(


我想学习如何在本例中使用Async/await方法,以及为类似功能采用的最佳方法是什么?谢谢,主要有两种方法

1.在getconnecteduser()方法的订阅中写入方法调用verifyuser()。这样,您将永远不会得到空值。 2.您可以使用承诺而不是可观察订阅。然后使用async/await延迟方法的执行

async userTasks() {
  const usersDetails = await this.layoutService.getConnectedUser().toPromise();
  this.layoutService.connectedUser.uid = usersDetails.uid;
  this.adminService.verifyUser(this.userUID).subscribe(
    (data) => {
      this.userStatus = data[0].status;
      this.userProfile = data[0].profil;
      console.log("userProfile" + JSON.stringify(data[0].profil));
      this.userExists = true;
    },
    (err) => {
      this.userExists = false;
    }
  );
}
  

尝试
switchMap
async userTasks() {
  const usersDetails = await this.layoutService.getConnectedUser().toPromise();
  this.layoutService.connectedUser.uid = usersDetails.uid;
  this.adminService.verifyUser(this.userUID).subscribe(
    (data) => {
      this.userStatus = data[0].status;
      this.userProfile = data[0].profil;
      console.log("userProfile" + JSON.stringify(data[0].profil));
      this.userExists = true;
    },
    (err) => {
      this.userExists = false;
    }
  );
}