Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 。然后在异步函数返回值之前调用_Javascript_Typescript_Promise_Async Await - Fatal编程技术网

Javascript 。然后在异步函数返回值之前调用

Javascript 。然后在异步函数返回值之前调用,javascript,typescript,promise,async-await,Javascript,Typescript,Promise,Async Await,我正在尝试从API端点检索authConfig。在我的应用程序组件中,我从服务请求该功能 this.userDetailService.getAuthConfig().then(config => { this.oauthService.configure(config); this.oauthService.initAuthorizationCodeFlow(); }); 然后在我的服务中,设置身份验证配置并将其返回到应用程序组件。我使用。然后在get

我正在尝试从API端点检索authConfig。在我的应用程序组件中,我从服务请求该功能

this.userDetailService.getAuthConfig().then(config => {
      this.oauthService.configure(config);
      this.oauthService.initAuthorizationCodeFlow();
    }); 
然后在我的服务中,设置身份验证配置并将其返回到应用程序组件。我使用
。然后在
getAuthConfig
上使用
,这样当我需要配置
oauthService
时,config对象就存在了。当我调试它时,我看到
.configure
被一个空对象调用。为什么在getAuthConfig返回值之前调用configure

 getEnvs(): Promise<any> {
      return this.http.get('/backend').toPromise();
    }

 async getAuthConfig(): Promise<any> {
      this.getEnvs().then((data) => {
        const env = data.env;
        const authConfig: AuthConfig = {
          loginUrl: env.authorizationEndpoint,
          redirectUri: env.redirectUris,
          clientId: env.clientId,
          scope: '',
          oidc: false
        };
        return (authConfig);
      });
    }
getEnvs():承诺{
返回此.http.get('/backend').toPromise();
}
异步getAuthConfig():承诺{
this.getEnvs().then((数据)=>{
const env=data.env;
常量authConfig:authConfig={
loginUrl:env.authorizationEndpoint,
重定向URI:env.redirectUri,
clientId:env.clientId,
范围:“”,
oidc:错
};
返回(authConfig);
});
}

您需要从
getAuthConfig
返回创建的承诺,以便
getAuthConfig
的调用方可以正确地等待在
getAuthConfig
中生成的承诺链:

 async getAuthConfig(): Promise<any> {
     return this.getEnvs().then((data) => {
   //^^^^^^
       // ...
     })
由于
getAuthConfig
是一个异步函数,因此您可以选择利用该函数进行清理:

async getAuthConfig(): Promise<AuthConfig> {
  const { env } = await this.getEnvs();
  return {
    loginUrl: env.authorizationEndpoint,
    redirectUri: env.redirectUris,
    clientId: env.clientId,
    scope: '',
    oidc: false
  };
}
async getAuthConfig():Promise{
const{env}=wait this.getEnvs();
返回{
loginUrl:env.authorizationEndpoint,
重定向URI:env.redirectUri,
clientId:env.clientId,
范围:“”,
oidc:错
};
}

我还必须返回(authConfig)还是返回getAuthConfig就足够了?不要将
then
wait
promise风格混用。
async getAuthConfig(): Promise<AuthConfig> {
  const { env } = await this.getEnvs();
  return {
    loginUrl: env.authorizationEndpoint,
    redirectUri: env.redirectUris,
    clientId: env.clientId,
    scope: '',
    oidc: false
  };
}