I have a execute method that is creating requests and call each of those urls to get responses and combined them using promise.all
, issue with below code is its duplicating both responses and adding it twice to combineResponses
, can anyone help here what went wrong or better approach to resolve combine responses with promise.all ?
main.ts
export class GetAccountBalanceController extends Controller {
private _request: IRequestURL[] = [];
constructor() {
super();
}
private async execute(@Request() request: ExpressRequest): Promise < any > {
const combinedResponses: IResponse[] = [];
if (request.body.includeSpecialty === true) {
const bRetSpecialty = await this.specialtyBalanceRequest(request);
this._request.push(bRetSpecialty);
try {
const specialtyResponse: IResponse = await makeRequest(this._request);
combinedResponses.push(specialtyResponse);
} catch (err) {
return Promise.reject(err);
}
}
if (request.body.getAccountBalanceRequest) {
const bRetPBM = await this.pmbBalanceRequest(request);
this._request.push(bRetPBM);
try {
const pbmResponse: IResponse = await makeRequest(this._request);
combinedResponses.push(pbmResponse);
} catch (err) {
return Promise.reject(err);
}
}
return await Promise.all(combinedResponses);
}
}
while working another thought was
private async execute(@Request() request: ExpressRequest): Promise<any> {
const combinedResponses: IResponse[] = [];
if (request.body.includeSpecialty === true) {
const bRetSpecialty = await this.specialtyBalanceRequest(request);
this._request.push(bRetSpecialty);
}
if (request.body.getAccountBalanceRequest) {
const bRetPBM = await this.pmbBalanceRequest(request);
this._request.push(bRetPBM);
}
try {
const _data: IResponse = await makeRequest(this._request);
combinedResponses.push(_data);
} catch (err) {
return Promise.reject(err);
}
return await Promise.all(combinedResponses);
}