我有一个execute方法,该方法创建请求并调用每个url以获取响应,并使用promise.all
将其组合起来,以下代码的问题是它复制了两个响应并将其两次添加到combineResponses
,有人可以在这里提供帮助吗?解决问题的更好方法或将promise.all与响应结合起来的方法?
main.ts
export class GetAccountBalanceController extends Controller {
private _request: IRequestURL[] = [];
constructor() {
super();
}
private async execute(@Request() request: ExpressRequest): Promise < any > {
const combinedResponses: IResponse[] = [];
if (request.body.includeSpecialty === true) {
const bRetSpecialty = await this.specialtyBalanceRequest(request);
this._request.push(bRetSpecialty);
try {
const specialtyResponse: IResponse = await makeRequest(this._request);
combinedResponses.push(specialtyResponse);
} catch (err) {
return Promise.reject(err);
}
}
if (request.body.getAccountBalanceRequest) {
const bRetPBM = await this.pmbBalanceRequest(request);
this._request.push(bRetPBM);
try {
const pbmResponse: IResponse = await makeRequest(this._request);
combinedResponses.push(pbmResponse);
} catch (err) {
return Promise.reject(err);
}
}
return await Promise.all(combinedResponses);
}
}
在工作时另一个想法是
private async execute(@Request() request: ExpressRequest): Promise<any> {
const combinedResponses: IResponse[] = [];
if (request.body.includeSpecialty === true) {
const bRetSpecialty = await this.specialtyBalanceRequest(request);
this._request.push(bRetSpecialty);
}
if (request.body.getAccountBalanceRequest) {
const bRetPBM = await this.pmbBalanceRequest(request);
this._request.push(bRetPBM);
}
try {
const _data: IResponse = await makeRequest(this._request);
combinedResponses.push(_data);
} catch (err) {
return Promise.reject(err);
}
return await Promise.all(combinedResponses);
}