Javascript Promise.all typscript不进行异步调用
我正在尝试进行大量异步API调用,并希望在Promise中处理。所有这些都返回并解决Javascript Promise.all typscript不进行异步调用,javascript,angular,typescript,es6-promise,angular-promise,Javascript,Angular,Typescript,Es6 Promise,Angular Promise,我正在尝试进行大量异步API调用,并希望在Promise中处理。所有这些都返回并解决 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
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 () {
});
}
if(this.selectedDomains.length>0){
for(设i=0;i可观测;
日志(消息:字符串,响应:对象):void;
deleteData(url:字符串,数据?:任意):可观察;
getData(url:string):可观察;
postData(url:string,data?:any,params?:any):可观察;
putData(url:string,data?:any,params?:any):可观察;
patchData(url:string,data?:any,params?:any):可观察;
headData(url:string):可观察;
静态ɵfac:ɵngcc0.ɵɵFactoryDef;
}
如果我没有看错,exportPolicies
通过调用promises.push(this.policyService.exportPolicies(…)返回一个可观察的
,您将一个可观察的
添加到名为承诺
的数组中。现在承诺。所有的
都不知道如何处理一个可观察的
,只是返回可观察的本身
要解决这个问题,您只需将可观察的
转换为承诺
:
promises.push(this.policyService.exportPolicies(...)).toPromise();
通过正确键入承诺
,您可以完全避免此问题,例如,将其声明为const promises:Promise[]=[]
。然后,您的TypeScript编译器甚至在执行:-)之前就已经抱怨过了。或者将您的观测值转换为承诺,然后使用承诺。all
,或者使用相当于
承诺的方法。所有
用于可观察的:
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>;
}
promises.push(this.policyService.exportPolicies(...)).toPromise();