我正在尝试编写可以处理承诺的通用函数。
所以我在 main.ts 中有承诺,一旦请求进来,我想将承诺传递给通用函数以使用 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)
}