Search code examples
javascriptangularangular-universalangular7

Angular 7: trying to delay router.navigate with setTimeout not working


I'm having an issue with my sign in handler redirecting to a page before the cookie with the user token is written. As a workaround, I tried adding a delay before the navigation, but it isn't working.

Why is the navigation happening before the timeout delay? (This is Angular 7 Universal.) I also tried setting the token in the delay function, but it didn't make a difference.

signInSuccessHandler() {
...
   const token = this.getToken();
        this.setToken(access_token);
        setTimeout(() => { this.delayNavigation(); }, 1000);
  }

  delayNavigation() {
    this._zone.run(() => this.router.navigate(['/people']));
  }

Solution

  • Thanks for the replies. I found the underlying cause, so I don't need the workaround anymore.

    A service was being instantiated before the token was available. The timeout didn't help because the issue had already happened.