我正在尝试编写可以处理承诺的通用函数。
所以我在main.ts中有一个Promise,一旦请求传入,我想将Promise.all传递给通用函数以执行这些Promise.all
我在commonFunction中得到了空的承诺数组。有点卡在这里需要帮助才能完成此任务。
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
}
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)
}
答案 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