Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

102
Visualizações
Waiting till component variable is set by subscribe

I have to calls in my component. The secound depends on the result from the first call.At the first call, I set the value for my component variable "locked". The secound call should be executed when the result is true --> locked = true. But it will reach the code, before the first call is finished and the value is set. How can I wait with the execution of my code, until the call "getByUsernamer" is finished.

Here is my code:

export class LoginComponent implements OnInit {
  
  errorMessage: string = "";
  isLocked = false;
  username: string | null = "";

  constructor(
    private authService: AuthService,
    private cookieService: CookieService,
    private router: Router,
    private jwtTokenService: JwtTokenService,
    private userService: UserService) {
  }

  ngOnInit(): void {
  }

  test() {
    this.userService.getUserByUsername(this.username)?.subscribe(
      {
        next: response => {
          let user: User = response;
          this.isLocked = user.locked
        }
      }
    );

    if (!this.isLocked) {
      this.router.navigate(['/home']).finally(
        () => this.userService.setLastLogin(this.username).subscribe());
    } else {
      this.errorMessage = "The user is locked."
    }
  }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can check the isLocked variable in the callback of the first call, so you are sure that you received the answer and the isLocked variable is set

export class LoginComponent implements OnInit {
  
  errorMessage: string = "";
  isLocked = false;
  username: string | null = "";

  constructor(
    private authService: AuthService,
    private cookieService: CookieService,
    private router: Router,
    private jwtTokenService: JwtTokenService,
    private userService: UserService) {
  }

  ngOnInit(): void {
  }

  test() {
    this.userService.getUserByUsername(this.username)?.subscribe(
      {
        next: response => {
          let user: User = response;
          this.isLocked = user.locked;

          // Moved here the test so it happens only after 
          // the first request  receive the answer
          if (!this.isLocked) {
            this.router.navigate(['/home']).finally(() => this.userService.setLastLogin(this.username).subscribe());
          } else {
            this.errorMessage = "The user is locked."
          }
        }
      }
    );
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Promises to resolve this issue. here is a link for your reference https://codecraft.tv/courses/angular/es6-typescript/promises/

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda