Skip to content

Acting on Futures (actOnFutures operator)

johnmcclean-aol edited this page Oct 13, 2015 · 6 revisions

The actOnFutures Operator ensures that the next operation is performed directly on the Stream of underlying Futures. The standard behaviour is act on results.

Comparing zipWithIndex

The normal behaviour of zipWithIndex is to act on the result of the previous stage.

Where the result is already available

LazyFutureStream.of("a","b","c","d")
                .zipWithIndex()
                .forEach(System.out::println)


["a",1l]
["b",2l]
["c",3l]
["d",4l]

Where the result is realised asyncrhonously

If the values are not already present, but computed or loaded asyncrhonously then completion order will determine index assigned to each result.

new LazyReact().react(()->load("a"),()->load("b"),()->load("c"),()->load("d"))
                .zipWithIndex()
                .forEach(System.out::println)


["c",1l]   <-- c completes first
["b",2l]   <-- b completes second 
["a",3l]   <-- a completes third
["d",4l]   <-- d completes fourth

Where the result is realised asyncrhonously and actOnFutures is used

When actOnFutures is used the index represents the index of the future task (and therefore the original order, or the order of the Futures input into that stage).

new LazyReact().react(()->load("a"),()->load("b"),()->load("c"),()->load("d"))
                .actOnFutures()
                .zipWithIndex()
                .forEach(System.out::println)


["c",3l]   <-- c completes first
["b",2l]   <-- b completes second 
["a",1l]   <-- a completes third
["d",4l]   <-- d completes fourth

actOnFutures operators

To be completed...

Clone this wiki locally