Ожидание ответа в функции входа в систему асинхроJavascript

Форум по Javascript
Ответить
Anonymous
 Ожидание ответа в функции входа в систему асинхро

Сообщение Anonymous »

У меня есть страница входа в мое приложение.
Это часть функции журнала: < /p>
this.loginService.doLogin(this.request).then(res => {
this.storage.set("auth-key", res.token);
this.authService.identity();
if (this.authService.isAuthenticated())
this.router.navigate(["expert-profile", this.authService.getCurrentUser().id], { queryParams: { lang: this.lang } });
return;
} else {
this.storage.remove("auth-key");
this.router.navigateByUrl("/login");
}
}).catch(err => {
this.storage.remove("auth-key");
});

identity() {
if (this.storage.get("auth-key")) {
this.fetch().then(res => {
this.userIdentity = {
id: res.id,
isAuthorized: res.isAuthorized
};
sessionStorage.setItem("userLoggedId", JSON.stringify(this.userIdentity.id));
this.authenticated = true;
}).catch(err => {
this.storage.remove("auth-key");
sessionStorage.removeItem("userLoggedId");
this.authenticated = false;
});

public async fetch(): Promise {
const response = this.http.get(environment.userDetailsUrl, { headers: new HttpHeaders({ "Authorization": "Bearer " + this.storage.get("auth-key") }) });
return await firstValueFrom(response);
< /code>
} < /p>
У меня есть следующая проблема. Чтобы войти в систему, я должен нажать кнопку входа в систему два раза, потому что в первый раз, когда ответ с функции Fetch, вероятно, не появится во времени (это.>

Подробнее здесь: https://stackoverflow.com/questions/796 ... sync-await
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Javascript»