问题
Is there any way to chain several observables but allowing the chain to complete at any time? I have three Observables, they all return booleans. However, I would only want to progress to the next observable in the chain if the current observable is false. The observables must progress upon the completion of the last one and where the completed value is false. Is this possible?
回答1:
You can setup an observable that control the flow and complete it when you are done. Also use zip operator - it will complete the whole flow if one of the observable(in our case the control one) is completed.
let control$ = new Rx.Subject();
let data$ = Rx.Observable.interval()
.map(x => x<10?true:false)
.do(flag => {
if(flag) control$.next(true);
else control$.complete();
});
Rx.Observable.zip(data$.filter(x=>x), control$.startWith(true), (x,y)=>x)
.subscribe(x=>console.log(x))
来源:https://stackoverflow.com/questions/44496395/rxjs-chain-observables-completing-at-any-point