首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Promise.all类型脚本不进行异步调用

Promise.all类型脚本不进行异步调用
EN

Stack Overflow用户
提问于 2020-08-04 15:06:57
回答 2查看 186关注 0票数 0

我在试着拒绝。的异步API调用,并希望在Promise.all中处理所有返回和解析。

代码语言:javascript
复制
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 () {
      });
    }

在这里,this.policyService.exportPolicies是一个异步API调用,但它从未执行&我看到的控制台msg已解析!!

在解析Promise.all数组的承诺中的所有异步API调用之后,我们如何使解析?

API调用详细信息:

代码语言:javascript
复制
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);
  }
代码语言:javascript
复制
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>;
}
EN

Stack Overflow用户

发布于 2020-08-04 15:27:45

要么将可观测值转换为承诺并使用Promise.all,要么对可观测数据使用等效的Promise.allforkJoin

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63249758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档