RXJS chain observables completing at any point

这一生的挚爱 提交于 2019-12-12 04:46:33

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!