-1

This is my class.

export MyClass {
    
    private readonly ServiceEndpoint: string = 
    "/xxx/xxx.DMS.UI.Root/Services/ConfigurationAPI.svc/";
    
    
    public async GetAllCompanies(): Promise<CompanyDto[]> {
    return fetchAsync(
        `${this.ServiceEndpoint}Company`,
        'GET'
        )
      .then(value => value.GetAllCompaniesResult);
  }

}

Presently, this method returns a Promise <CompanyDto[]>. How do I rewrite it so that it returns only the result CompanyDto[]?

  • You cannot. Fetching something over the network takes some time and is asynchronous, you get the results only later, it is impossible to immediately return them. – Bergi Jun 04 '23 at 14:18

1 Answers1

-1

I think you are missing await keyword here. Can you please try using await?

export MyClass {

  private readonly ServiceEndpoint: string = 
    "/xxx/xxx.DMS.UI.Root/Services/ConfigurationAPI.svc/";

  public async GetAllCompanies(): Promise<CompanyDto[]> {
    let result = await fetchAsync(
      `${this.ServiceEndpoint}Company`,
      'GET'
    )
    .then(value => value.GetAllCompaniesResult);

    return result; 
  }
}
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41