I have an API call which ends in a boolean check. I'd like to create new users but if an e-mail is exists in the db i want to set canAddUser = false. I do the following:
  canAddUser: boolean;
  setNewUserValues() {
    if (this.formUser.email) {
      this.checkExistingEmail();
    }
  }
  checkExistingEmail() {
    this.userService.findPrivilegedUsersUsingGET().subscribe(users => {
      const mailIndex = users.findIndex(user => user.email === this.formUser.email);
      mailIndex !== -1 ? this.canAddUser = false : this.canAddUser = true;
      console.log(this.canAddUser);
    });
  }
If I log the boolean to the console where I am currently doing, I get the right value. However if I log it elsewhere it's undefined first! then if i trigger the button that fires setNewUserValues() it gets the value right again. What am I missing so badly?
EDIT
Somewhat solved my issue.
  setNewUserValues() {
    if (this.formUser.email) {
      this.checkExistingEmail();
    }
  }
  checkExistingEmail() {
    this.userService.findPrivilegedUsersUsingGET().subscribe(users => {
      this.mailIndex = users.findIndex(user => user.email === this.formUser.email);
    });
    this.validateEmail(this.mailIndex);
    console.log(this.canAddUser);
  }
  private validateEmail(index: number) {
    index !== -1 ? this.canAddUser = false : this.canAddUser = true;
  }
If I pass an e-mail that's existing i got the correct value but if i pass one that's not in the db yet i get false value first then if i trigger it once more then the value is fine again.
 
     
     
     
    