Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

89
Views
Correctly use of Promises and await/async functions

I'm working in a Backend For Frontend (BFF) that receive a request by front end, the bff request an another api and with responses, creates the object to return to front end.

I would like to know if i'm using promises and await call correctly, there are two situations:

const [affiliates, { content }]: [
  { content: AffiliateType[] },
  IManagerTypeList,
] = await Promise.all([
  await this.affiliatesService.getAffiliates(
    { clientId: client?.id },
  ),
  await this.managersService.getManagers(
    { clientId: client?.id },
  ),
])

getAffiliates and getManagers are async methods that call another api. The content const i return directly to front end. With affiliates, i do the follow:

const unresolvedTransformedAffiliateToReturn = affiliates.content.map(
  async (affiliate): Promise<AffiliateType> => {
    const addressInfo: AddressType =
      await this.addressService.getAddressesById(
        affiliate.addressId,
      )

    const managerInfo: ManagersType =
      await this.managersService.getManagers(
        affiliate.managerId,
      )

    return {
      id: affiliate.id,
      name: affiliate.name,
      clientId: affiliate.clientId,
      managerId: affiliate.managerId,
      addressId: affiliate.addressId,
      address: addressInfo,
      manager: managerInfo,
    }
  },
)

const transformedAffiliateToReturn = await Promise.all(
  unresolvedTransformedAffiliateToReturn,
)

What is going here. affiliates.content is a array of object. I need to use some variables of affiliate to call getAddressesById and getManagers endpoints (also asyncs), like addressInfo and managerInfo, then, use this return to return to front end.

The final return to front end is:

{
  managers: content,
  affiliates: transformedAffiliateToReturn,
}

All works correctly. I'm only wondering if it's the right way to use promise.all and async/wait functions.

Some opinions or suggestions will be appreciate. Thanks!

EDIT: Example of getAffiliates request:

getAffiliates<R>(id: number, api?: string): Promise<R> {
  return this.customHttpService.mountRequest(
    RequestTypeEnum.GET,
    `/endpoint/${id}`,
    null,
    null,
    null,
    api,
    true,
)

}

mountRequest scope:

async mountRequest<P, B, H, R>(
  requestType: RequestTypeEnum,
  endpoint: string,
  params?: P,
  body?: B,
  headers?: H,
  api?: string,
  ignoreNotFoundError?: boolean,
  ): Promise<R> {
    const defaultApi: string = api ? api : 
    let request: Observable<AxiosResponse<R>>
    ...
  }
)
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!