In a stream expression, you can call yield()
to emit a value, and
await()
to wait for a value from a promise. To have your stream
wait for values from another stream or channel, call
awaitNext()
; you can also use awaitNext
when you are writing an
async
. You can also use a simple for
loop to consume all future
values from a stream or channel.
The lower-level interface to consume values from a stream is by using
nextThen from the channel interface.
Streams come in both "lazy" and "eager" varieties. If lazy=TRUE
,
a stream starts idle, and does not process anything
until it is woken up by a call to its channel's nextThen
. It will
pause after reaching yield
if there are no more outstanding
requests. If lazy=FALSE
, a stream will begin executing
immediately, not pausing on yield
, possibly queuing up emitted
values until it needs to await
something.
(For comparison, in this package, gen are lazy in that they do
not start executing until a call to nextOr
and pause
immediately after yield
, while async blocks are eager,
starting at construction and running until they hit an await
.)
Like its coroutine counterparts, if stream
is given a function
expression, like stream(function(...) ...)
, it will return a
"stream function" i.e. a function that constructs a stream object.