角度:了解如何等待回复

时间:2019-07-07 23:35:51

标签: angular promise

感谢您抽出宝贵的时间对此进行研究。我已经阅读了多个有关如何订阅响应的问题,但是在我的程序上下文中却难以做到。

在这里,我要求Okta提供用户数据(足够简单)。接下来是将结果映射到数组,然后将用户名分配给第一个值,将用户组分配给userGroups。但是,正如预期的那样,Angular在响应进入之前会触发该代码。userClaims!= null是防止Angular错误的权宜之计。

我的具体问题:如何使用这些变量构造此请求?

async ngOnInit() {
this.isAuthenticated = await this.oktaAuth.isAuthenticated();
this.oktaAuth.$authenticationState.subscribe(
  (isAuthenticated: boolean)  => this.isAuthenticated = isAuthenticated
  );

const userClaims = await this.oktaAuth.getUser();


if (userClaims != null) {
this.claims = Object.entries(userClaims).map(entry => ({ claim: entry[0], value: entry[1] }));
this.userName = this.claims[1].value;
this.userGroups = userClaims.groups;

console.log('ADMIN COMPONENT: userName ' + this.userName + ' is a member of : ' + this.userGroups);
}}}

编辑:这是诺言

import { Router, NavigationExtras } from '@angular/router';
import { OktaConfig } from '../models/okta.config';
import { UserClaims } from '../models/user-claims';
import { Observable } from 'rxjs';
export declare class OktaAuthService {
    private auth;
    private router;
    private oktaAuth;
    private config;
    private observers;
    $authenticationState: Observable<boolean>;
    // $userState: Observable<boolean>;

    constructor(auth: OktaConfig, router: Router);
    /**
     * Checks if there is an access token and id token
     */
    isAuthenticated(): Promise<boolean>;
    private emitAuthenticationState;
    /**
     * Returns the current accessToken in the tokenManager.
     */
    getAccessToken(): Promise<string | undefined>;
    /**
     * Returns the current idToken in the tokenManager.
     */
    getIdToken(): Promise<string | undefined>;
    /**
     * Returns user claims from the /userinfo endpoint if an
     * accessToken is provided or parses the available idToken.
     */
    getUser(): Promise<UserClaims | undefined>;
    // private emitUserState;
    /**
     * Returns the configuration object used.
     */
    getOktaConfig(): OktaConfig;
    /**
     * Launches the login redirect.
     * @param fromUri
     * @param additionalParams
     */
    loginRedirect(fromUri?: string, additionalParams?: object): void;
    /**
     * Stores the intended path to redirect after successful login.
     * @param uri
     * @param queryParams
     */
    setFromUri(uri: string, queryParams?: object): void;
    /**
     * Returns the referrer path from localStorage or app root.
     */
    getFromUri(): {
        uri: string;
        extras: NavigationExtras;
    };
    /**
     * Parses the tokens from the callback URL.
     */
    handleAuthentication(): Promise<void>;
    /**
     * Clears the user session in Okta and removes
     * tokens stored in the tokenManager.
     * @param uri
     */
    logout(uri?: string): Promise<void>;
    /**
     * Scrub scopes to ensure 'openid' is included
     * @param scopes
     */
    scrubScopes(scopes: string): string;
}

1 个答案:

答案 0 :(得分:2)

我希望这会有所帮助。 请记住,异步/等待是与诺言一起使用的语法,使诺言变得干净和“轻松”。

我在https://stackblitz.com/edit/github-ijnmcd做了一个非常简单的例子 我在哪里叫“跳舞小丑” 3次,一次许诺,另一次异步/等待。使用该示例应该很容易实现您的解决方案。

请记住使用try {} catch(e){}来捕获异步/等待错误。

查看以下内容:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

顺便说一句,请记住HttpClient是可观察的,这意味着您必须订阅它,如果要使用async / await,则需要使用toPromise()而不是subscribe();

希望有帮助。