首页 > 解决方案 > 拥有 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 
}

标签: javascripttypescriptpromise

解决方案


推荐阅读