How to finalize nested Observables?

后端 未结 1 582
轮回少年
轮回少年 2021-01-26 03:59

Please, take a look at the following try for better understanding:

this.loading = true;
obs.pipe(
   finalize(() => this.loading = false) //If set to false he         


        
1条回答
  •  遥遥无期
    2021-01-26 04:23

    You should use the switchMap operator to combine the two observables into one:

    this.loading = true;
    loadParent().pipe(
      switchMap(parent => {
        if (person.children) {
          return loadChildrenOf(parent).pipe(
            map(children => ({ parent, children })
          )
        } else {
          return of({parent, children: [] })
        }
      }),
      finalize(() => this.loading = false)
    ).subscribe(({ parent, children }) => {
      // Do stuff with the parent and its children
    });
    

    0 讨论(0)
提交回复
热议问题