RXJS 链 observables 在任何时候完成 [英] RXJS chain observables completing at any point

查看:45
本文介绍了RXJS 链 observables 在任何时候完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法可以将多个 observable 链接起来,但允许链接随时完成?我有三个 Observable,它们都返回布尔值.但是,如果当前 observable 为假,我只想前进到链中的下一个 observable.observables 必须在最后一个完成后进行,并且完成的值为 false.这可能吗?

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?

推荐答案

您可以设置一个 observable 来控制流程并在完成后完成它.还可以使用 zip 运算符 - 如果其中一个可观察对象(在我们的例子中是控件)完成,它将完成整个流程.

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))

这篇关于RXJS 链 observables 在任何时候完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆