Synchronising
Chains are inherently asynchronous. If you’re working with several, there might be a need to synchronise them in some manner. This is a way of aggregating data into a common array or object.
combineLatestToArray
combineLatestToArray monitors two or more chains, storing values as they happen to an array. Whenever a new value is emitted, the whole array is sent out, containing current values from each source, or undefined if not yet emitted.
The tempo of this stream will be set by the fastest source stream. syncToArray in contrast has a pace determined by slowest source, only sending when each source has produce a new value compared to last time.
There are a few options to determine what happens when a source completes. See the API docs for more information. By default as soon as one source finishes, the combined stream finishes.
combineLatestToObject
combineLatestToObject is essentially the same as combineLatestToArray()
however it returns an object of values rather than array.
syncToArray
syncToArray waits for all sources to produce a value, sending the combined results as an array.
After sending, it waits again for each source to send at least one value. This means that the pace of the combined result will be determined by the slowest source. In contrast combineToArray/combineToObject
have their pace set by the fastest source.
As soon as one source finishes, the synchronised generator will finish. You can tweak the behaviour when sources end by providing options, documented here.