javascript - 拥有 Promise.all 的正确方法是什么?
问题描述
我有两个基于请求的调用,它可能是单个或两个 api 调用,所以我在下面的代码中实现了不确定这是否是使用 promise.all 的正确方法,也想将错误返回给用户以防任何承诺失败。
execute
功能从路线执行。如果有更好的实施方法,我将不胜感激。
main.ts
public async execute(@Request() request: express.Request): Promise<[any] | any> {
if (request.body.lob === "credit") {
return this.getCardDetails(request);
}
if (request.body.lob === "individual") {
return this.getAccountDetails(request);
}
return Promise.all([this.getCardDetails(request), this.getAccountDetails(request)]);
}
@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
}
解决方案
推荐阅读
- javascript - 为什么要在 addEventListener 中调用函数
- jqchart - jqchart:显示没有点/点的线?
- spring-boot - 使用 Swagger + Java REST API
- regex - How to retain all columns in pandas while using regex
- reactjs - Mobx 状态树中的重复节点
- timestamp - 如何在雅典娜中使用时区将毫秒转换为时间戳
- java - Java俄罗斯方块 - 无法设置将关键设置保存到配置文件中
- php - 无法在 Kubernetes 上将 HTTP 与 php 映像一起使用
- r - 一个简单的可重现示例,用于在 R 中的自定义函数中将参数传递给 data.table
- security - 每个用户的秘密 JWT