0

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

    }
hussain
  • 6,587
  • 18
  • 79
  • 152
  • What good is `this._request`? Why should it be shared across method calls on the same instance? – Bergi Oct 02 '18 at 15:52
  • @Bergi `makeRequest` takes array of url and body and making underlying calls to api using axios – hussain Oct 02 '18 at 15:53
  • 1
    `try { … } catch (err) { return Promise.reject(err); }` makes no sense, you already are in an `async function` which does just that. Also [don't do `return await`](https://stackoverflow.com/a/43985067/1048572). And why are you even using `Promise.all`? `combinedResponses` is not an array of promises. – Bergi Oct 02 '18 at 15:54
  • @Bergi any better approach to achieve this task – hussain Oct 02 '18 at 16:01
  • Yes, `makeRequest` may take an array, but WTH did you make `_request` an instance property, initialised once in the constructor? Why is it not a local variable to the `execute` method? Or since you are complaining about duplication, probably it even should be two variables local to each `if` block?! – Bergi Oct 02 '18 at 16:01
  • @Bergi i added more code to question that i thought resolve the issue – hussain Oct 02 '18 at 16:07
  • No, it is really unclear what you actually *want* to do, what all the functions do that you call, what result you did expect from the current code, and what you are getting instead. – Bergi Oct 02 '18 at 17:08

0 Answers0