I need to handle multiple async API calls using Promise.all after they have all returned and resolved.
if(this.selectedDomains.length > 0) {
for(let i=0; i<this.selectedDomains.length; i++){
promises.push(
this.policyService.exportPolicies(this.selectedDomains[i].id, this.config['data'].policies)
);
}
//wait for all exportPolicies calls to finish
Promise.all(promises).then(function () {
console.log("ALL resolved !!");
let successMsg = this.translate.instant("policy.resources.export_policy_success",
[this.config['data'].policies.length, this.selectedDomains.length]);
this.messageHelperService.showSuccess({hide: true, message: successMsg});
}
).catch(function () {
});
}
In the code above, this.policyService.exportPolicies is an async API call that never gets executed, but I still see the console message "ALL resolved !!"
How can we ensure that Promise.all resolves only after all the async API calls in the promises array have been resolved?
Here are the details of the API call:
export class PolicyService {
constructor ( private baseService : BaseService ) {
}
exportPolicies(domainId, policyIds) : Observable<import("@angular/common/http").HttpEvent<any[]>>{
let url = COMMON.LEGACY_API_PATH + `policy/exportPolicy/${domainId}`;
return this.baseService.postData(url, policyIds);
}
export declare class BaseService {
private http;
constructor(http: HttpClient);
handleError<T>(operation?: string, result?: T): (error: any) => Observable<T>;
log(message: string, response: object): void;
deleteData(url: string, data?: any): Observable<import("@angular/common/http").HttpEvent<any[]>>;
getData(url: string): Observable<any[]>;
postData(url: string, data?: any, params?: any): Observable<import("@angular/common/http").HttpEvent<any[]>>;
putData(url: string, data?: any, params?: any): Observable<import("@angular/common/http").HttpEvent<any[]>>;
patchData(url: string, data?: any, params?: any): Observable<import("@angular/common/http").HttpEvent<any[]>>;
headData(url: string): Observable<any[]>;
static ɵfac: ɵngcc0.ɵɵFactoryDef<BaseService, never>;
}