正确使用Promise和await/async函数

发布于 2025-01-14 17:00:23 字数 2330 浏览 3 评论 0原文

我正在前端后端(BFF)中工作,它接收前端的请求,bff 请求另一个 api 并通过响应创建对象以返回到前端。

我想知道我是否正确使用了promise和await调用,有两种情况:

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

getAffiliatesgetManagers是调用另一个api的异步方法。 content const 我直接返回到前端。对于附属机构,我执行以下操作:

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,
)

这里发生了什么。 affiliates.content 是一个对象数组。我需要使用附属机构的一些变量来调用 getAddressesByIdgetManagers 端点(也是异步的),例如 addressInfo 和 managerInfo,然后使用此返回返回到前端。

最终返回前端的是:

{
  managers: content,
  affiliates: transformedAffiliateToReturn,
}

一切正常。我只是想知道这是否是使用 Promise.all 和 async/wait 函数的正确方法。

一些意见或建议将不胜感激。谢谢!

编辑: getAffiliates 请求示例:

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

}

mountRequest 范围:

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>>
    ...
  }
)

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>>
    ...
  }
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文