[英]Why the operator forkJoin does not work?
I'm trying to add two observable. 我试图添加两个可观察到的。 But the console does not display anything.
但是控制台不显示任何内容。
var s = Rx.Observable.of(100, 200, 300);
var s1 = Rx.Observable.interval(1000);
var sub = Rx.Observable.forkJoin(s, s1)
sub.subscribe(x => console.log(x));
For the case of two synchronous observables there is no problem 对于两个同步可观察物,没有问题
forkJoin
emits only when all its source Observables emit at least one item and all of them complete. 仅当其所有源Observable发出至少一项且它们全部完成时,
forkJoin
才发出。 Using Observable.of
is fine. 使用
Observable.of
很好。 It emits all its values and completes immediately. 它发出所有值并立即完成。 But
Observable.interval
never completes on itself (you might want to chain it with take(N)
for example. 但是
Observable.interval
永远不会自行完成(例如,您可能希望将其与take(N)
。
var s1 = Rx.Observable.interval(1000).take(1);
// Now both source Observables complete so `forkJoin` will complete as well
var sub = Rx.Observable.forkJoin(s, s1);
If you look up the forkJoin() function in the official documentation you will see: 如果您在官方文档中查找forkJoin()函数 ,将会看到:
forkJoin will wait for all passed Observables to complete and then it will emit an array with last values from corresponding Observables.
forkJoin将等待所有传递的Observable完成,然后它将发出一个数组,其中包含来自相应Observable的最后一个值。
If you look at the interval() function 如果您看一下interval()函数
interval returns an Observable that emits an infinite sequence of ascending integers ....
interval返回一个Observable,它发出无限个递增整数序列。
So your forkJoin works fine, it will just never console.log()
anything, because the interval function doesn't end. 因此,您的forkJoin可以正常工作,它将永远不会
console.log()
任何东西,因为interval函数不会结束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.