Angular2. How can I check if an observable is completed?

后端 未结 6 535
轮回少年
轮回少年 2020-12-09 08:39

In my page there is a button that generates a report. That report needs data that is loaded using a http call to a rest endpoint when the page is loaded, but I do not have a

6条回答
  •  庸人自扰
    2020-12-09 09:33

    In this kind of scenarios it is very useful to use concatMap operator to guarantee to execute the next operation only when the previous has done.

    loadCompanies(): void {
        this._companyService.getCompanies()
        .concatMap(companyList => this.getObservableGenerateReport(companyList))
        .subscribe(
            response => {
                this.companiesModel = response;
            },
            err => console.log(err)
        );
    }
    
    
    //Create observable to generate the report
    getObservableGenerateReport(response: any): Observable {
    
        return Observable.create(observer => {
    
          if (generateReport().isSuccessful) {
            observer.next(myReportList);
            observer.complete();
          } else {
            console.log(err, 'Ups, something was wrong!');
            observer.next({});
            observer.complete();
          }
    
        });
      }
    

提交回复
热议问题