是否可以将承诺传递给其他中间件并在那里解决?

时间:2018-08-13 20:28:10

标签: javascript angular typescript es6-promise

我正在尝试编写可以处理承诺的通用函数。

所以我在main.ts中有一个Promise,一旦请求传入,我想将Promise.all传递给通用函数以执行这些Promise.all

我在commonFunction中得到了空的承诺数组。有点卡在这里需要帮助才能完成此任务。

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)

 }

1 个答案:

答案 0 :(得分:1)

public async execute(@Request() request: express.Request): Promise < [any] | any > {
     const promises: any = [];
     promises.push(this.getAccountDetails(request), this.getCardDetails(request));
     return responseCollector(request, promises);
}

请勿使用.apply