Varma GRK
Varma GRK

Reputation: 77

Angular: Console.log(11) is printed first then console.log(1). why?

I have a simple submit method, where Console.log(11) is printed first then console.log(1). I need why it is behaving in that way.

submit(value) {

    this.secServise.getUserById(this.currentUser.mgId).subscribe( uAddrs => {
      this.tempUser = uAddrs;
      console.log(1);
    });

    if (value[this.keyPwd] !== value[this.keyCPwd]) {
      this.messageService.add({
         severity: 'error',
         summary: 'Password and Confirm Password should match',
         detail: '' 
      });
    } else {
      console.log(11);
    }
}

Upvotes: 0

Views: 94

Answers (1)

Mathyn
Mathyn

Reputation: 2600

This has nothing to do with Angular but rather with the way Javascript works. Basically your code is not synchronous like you would expect because your getUserById call is most likely asynchronous.

This means Javascript will not wait until the getUserById callback function is called. Instead it will just continue with the next line of code which apparently in this case is the console.log(11) line since the if statements evaluates as false.

Upvotes: 1

Related Questions