How can I avoid multiple nested subscriptions using RxJS operators?

前端 未结 4 1615

I am working on a file encryption and upload class using Angular. Many of these operations are async and therefore the methods I wrote are returning RxJS Observables.

<
4条回答
  •  -上瘾入骨i
    2020-12-11 04:37

    I think chaining your observables would do it, you can do it with flatMap (alias for mergeMap) maybe - https://stackoverflow.com/a/37777382/9176461 and RxJS Promise Composition (passing data)

    As in my comment mentoined, something like the following should work (pseudo code):

    public upload(file) {
        const gen = this.indexGenerator(); // generator function
    
        return Rx.Observable.just(file).pipe(
             mergeMap(this.prepareUpload),
             mergeMap(this.encryptData),
             mergeMap(this.prepareEncDataUpload),
             mergeMap(this.prepareEncDataUpload),
             .... )
    }
    

提交回复
热议问题