Lazy Execution
When operations are added to a pipeline, they are pushed into an internal data structure but they are not executed until the pipeline's run()
method is invoked.
When run()
is invoked, each operation is invoked in a cascading fashion, with the output of the current operation being provided as input to the next.
The return value of run()
is an asynchronous iterable value which can then be used.
At this stage no processing has happened yet, because of the lazy nature of asynchronous iterables.
Only when the output iterable is iterated in user code will the actual processing happen.
Example
This example shows a how to use a custom iterable capable of generating all integer numbers. Iterating it would lead to an infinite loop.
When the pipeline is executed with run()
no infinite loop happens, because the pipeline stages are only executed and no iteration occurs.
Iteration happens when the result
value is then iterated in your code.