gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:19:24 25 4
gpt4 key购买 nike

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

所以我在 main.ts 中有 promise ,一旦收到请求,我想将 promise 传递给常用函数,以使用 Promise.all 执行这些 promise

我在 commonFunction 中得到了空的 promise 数组。有点被困在这里需要帮助来完成这项任务。

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)

}

最佳答案

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

关于javascript - 是否可以将 promise 传递给其他中间件并在那里解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51830085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com