In an attempt to streamline my code, I am working on a generic function that can manage promises efficiently.
Currently, I have promises set up in main.ts and when a request is received, I would like to pass these promises to the common function for execution using Promise.all.
However, I am encountering an issue where the array of promises passed to commonFunction is empty. Any guidance on resolving this challenge would be greatly appreciated.
main.ts
public async execute(@Request() request: express.Request): Promise < [any] | any > {
const promises: any = [];
promises.push.apply(this.getAccountDetails(request), this.getCardDetails(request));
return responseCollector(request, promises);
}
@Post('getAccountDetails')
private async getAccountDetails(@Body() request: any): Promise < any > {
const accountDetails: any = await axios.post(
url, request.body);
return accountDetails;
}
@Post('getCardDetails')
private async getCardDetails(@Body() request: any): Promise < any > {
// process cardDetails Call and get response
}
commonFunction.ts
export function responseCollector(expressReq, promises) {
console.log("requestBody>>>", expressReq.body);
console.log("promioses>>>>", JSON.stringify(promises));
if (expressReq.body.lob === "credit") {
return promises.Object
}
if (expressReq.body.lob === "account") {
return promises.object
}
return Promise.all(promises)
}