2

in RxJava there is the Observable.toBlocking() operator to retrieve the data of the observable synchronously. I can not find a similiar operator for RxJS. I want to use this operator to improve my code with Rx and without using another functional programming library...

Tim Joseph
  • 847
  • 2
  • 14
  • 28

1 Answers1

2

see schedulers

If you do not provide the scheduler, RxJS will pick a default scheduler by using the principle of least concurrency. This means that the scheduler which introduces the least amount of concurrency that satisfies the needs of the operator is chosen.

As long as you don't specify a scheduler RxJS itself picks a blocking scheduler / resolves the observable synchronously (immediate scheduler) as you can see in this jsbin - the first observable completes before the second is started.

If you want to control the level of concurrency explicitely, you could do this by passing a specific scheduler to the operator that supports this option.

Niklas Fasching
  • 1,326
  • 11
  • 15
  • But I still had to use .subscribe instead of the result as with RxJava... :( – Tim Joseph Dec 17 '15 at 18:28
  • Gotcha. I don't know of any such operator for RxJS but (ignoring the merit of doing so) it would be easy enough to implement - as long as you're dealing with purely synchronous / immediately scheduled observables (see [jsbin](http://jsbin.com/fuvakajoba/edit?js,console)). For asynchronous observable it would be a bad idea to do so [anyway](http://stackoverflow.com/questions/23231909/collect-rxjs-observable-to-array) – Niklas Fasching Dec 17 '15 at 18:47
  • I would suggest against doing this though (e.g. this will break non-obviously if asychronity gets introduced) – Niklas Fasching Dec 17 '15 at 18:54