Consuming data
Iterating
The return result of run()
is a regular Javascript asynchronous generator. This means you can use a for .. of
loop or step through it manually.
In the above pattern, execution of code after the for await
unless the chain ends. In this case, since the source is an event, it will never end.
An alternative means of access is using ixfx’s asCallback , which, if you don’t prefix it with await
, continues execution.
You can also optionally provide a third parameter to be notified asynchronously when the chain is completed:
Reading values
The for..of
pattern is not always appropriate. In some cases you want to essentially read the value from the chain on demand. asValue handles this, providing a function to read the latest value from the chain.
When initialising, you can also provide an initial value. This is useful for chains where the source is based on an event that perhaps has not fired yet, but you still want a usable value when reading.
Accumulating
All data from a chain can be accumulated into an array:
If the chain is infinite (never ends), be sure to provide some limits:
asArray
returns a new array, and is meant to be used with await
so execution only continues when the data is fully read.
Alternatively you can add to an existing array over time using addToArray .