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