I am trying to create generic function that can take functions as an array and execute it using promise.all , its throwing undefined exception.
main.ts
public async execute(@Request() request: express.Request): Promise < [any] | any > {
const promises = [];
promises.push(this.getAccountDetails, this.getCardDetails);
return sendResponse(request, promises);
}
@Post('getAccountDetails')
private async getAccountDetails(@Body() request: any): Promise < any > {
// process retrieveData Call and get response
}
@Post('getCardDetails')
private async getCardDetails(@Body() request: any): Promise < any > {
// process cardDetails Call and get response
}
commonFunction.ts
export function sendResponse(expressReq, promises) {
const promiseCollector: any = [];
promises.forEach(function(func: any) {
if (expressReq.body.lineOfBusiness === "account") {
promiseCollector.push(func(expressReq.body));
}
if (expressReq.body.lineOfBusiness === "credit") {
promiseCollector.push(func(expressReq.body));
}
});
return Promise.all(promiseCollector);
}