V
V
Vladimir Golub2020-05-07 09:44:27
typescript
Vladimir Golub, 2020-05-07 09:44:27

What is the correct way to return the Promise of an object?

There is a function, it returns a Promise

getTotalProducts(): Promise<number> {
    return this.productsRepository.count();
  }


I'm trying to return the result of executing this function in an object with an interface
getTestProducts(): GetTestProducts {
    return {
      total: this.productsService.getTotalProducts()
    };
  }

export interface GetTestProducts {
  total: Promise<number>
}


As a result I get
{
    "total": {}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Golub, 2020-05-07
@RazerVG

Decision

async getProducts(page: number, perPage: number): Promise<getProducts> {
    const products = await this.productsRepository.find({
      relations: ["category", "photos"] }
    );

    const total = await this.productsRepository.count();

    return {
      products,
      total
    }
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question