how to wait until localStorage.setItem finishes in angular 4
You are wrong , all localStorage calls are synchronous. Probably you can add a check before navigating to the next page.
localStorage.setItem('currentUser', JSON.stringify(response.body)); this.router.navigateByUrl('/');
In case anyone is getting the same kind of a issue... the reason for this was I have created a variable in my service class to save the current user and set the value for that variable inside the constructor. So, in the login page when the authenticate
method gets called it tries to initialize the variable but since user is not logged in yet it is still null
. That was causing the issue.