如何将异步函数传递给另一个函数并执行它?

时间:2018-08-14 14:59:39

标签: javascript typescript callback es6-promise

我正在尝试创建通用函数,该函数可以将函数作为数组并使用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);

 }

0 个答案:

没有答案