I want to make two HTTP calls.
Below is my TypeScript Code
CheckLogIn() {
this.http.get<boolean>(this.baseUrl + 'api/LogIn/CheckLogIn/' + this.StaffCode).subscribe(result => {
setTimeout(() => {
if (result == true) {
this.GetUserName();
sessionStorage.setItem("UserID", this.StaffCode);
this.router.navigate(['/log-tracker']);
}
}, 5000)
}, error => console.log(error));
}
GetUserName() {
this.http.get(this.baseUrl + 'api/Common/GetUserName/' + sessionStorage.getItem("UserID"), { responseType: 'text' }).subscribe(result => {
console.log(result);
sessionStorage.setItem("UserName", result);
}, error => console.log(error));
}
Here Inside CheckLogin() I am calling an endpoint and within response of this call I am invoking another one (GetUserName) and then redirecting to another page.
But checkLogin Does not wait for GetUserName to finish and redirect to page before second call finishes its work , hence session username is always null .
I tried using SetTimeout Function but it does not work here , is there any other way to put a delay before redirection , or any way to make first call wait until second call finishes its work ?
this.router.navigate(['/log-tracker']);this has to be withinGetUserNamesubscribe method.switchMap