The examples in the RxJS README seem to suggest we have to subscribe to a source. In other words: we wait for the source to send events. In that sense, sources seem to be push-based: the source decides when it creates new items.
This contrasts, however, with iterators, where strictly speaking new items need only be created when requested, i.e., when a call is made to next()
. This is pull-based
behavior, also known as lazy generation.
For instance, a stream could return all Wikipedia pages for prime numbers. The items are only generated when you ask for them, because generating all of them upfront is quite an investment, and maybe only 2 or 3 of them might be read anyway.
Can RxJS also have such pull-based behavior, so that new items are only generated when you ask for them?
The page on backpressure seems to indicate that this is not possible yet.
next
does not imply that the new items are only created when necessary as you say, it just means that they are requested (and provided as iterators are synchronous) at that time. Typical example are iterators based on arrays. The array (and the values inside) already existed before you call the iterator to enumerate its values. Actually you can think of the array as a buffer holding the values, and yournext
operator acts as would a controlled Rx.Observable withrequest(1)
. – Cistaceous