我正在尝试创建通用函数,该函数可以将函数作为数组并使用promise.all(抛出未定义的异常)执行它。
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);
}