简体   繁体   English

为什么操作员forkJoin不起作用?

[英]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));

live example here 这里的例子

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.

相关问题 为什么|| 操作员不能在 JavaScript 中工作? - Why does the || operator not work in JavaScript? Javascript:为什么 % 运算符作用于字符串? - Javascript : Why does % operator work on strings? 为什么我的if语句与else if而不是OR运算符一起工作 - Why does my if statement work with an else if, but not an OR operator 为什么“+”运算符可以将字符串解析为数字? - Why does the “+” operator work in parsing string to number? Javascript:为什么前缀运算符只能与模数一起使用,而不能与后缀运算符一起使用? - Javascript: Why does prefix operator work with modulus but not postfix operator? Javascript:逗号运算符,var和作用域-为什么它按它的方式工作? - Javascript: Comma operator, var, and scope - why does it work the way it does? 如果字符串在 Javascript 中是不可变的,为什么 += 运算符会以这种方式工作? - If strings are immutable in Javascript, why does the += operator work the way it does? 为什么模运算符在 javscript 的 FOR 循环中的工作方式不同? - Why does the modulo operator appear to work differently in FOR loops in javscript? JavaScript中令人困惑的逗号运算符。 为什么以这种方式工作? - Confusing comma operator in JavaScript. Why does it work in such manner? 为什么此用于在jQuery中切换动画的三元运算符不起作用? - Why does this ternary operator for toggling animation in jQuery not work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM