Chaining operations ¶
Sync or Async¶
To chain operations synchronously, you typically write statements one after the other, using the results of the previous statements to perform new statements:
These operations can also be executed asynchronously:
These pieces of code execute the asynchronous operations, but they are not linked together: both actions will start at the same time.
What we want here is to execute the second operation when the first one provides its result (here listen when the animation finishes). We will see below how to chain these operations asynchronously.
In the following sections, the mentioned
Future class corresponds to the
When using the
Future class in your code, make sure to import
What is a future
Future is an object that wraps an operation asynchronously.
It allows you to perform asynchronous operations while writing code in a sequential way.
Future class is mainly used to perform the following operations
- create objects like actions and resources,
- handle an action execution (result, cancellation and error),
- chain several action executions / resource creations.
State of a future
Future has 3 states: it can provide a result, it can be cancelled or it
These states correspond to the different end states of the wrapped operation.
If the wrapped operation:
- returns a value, the
Futurewill provide this value when available,
- is cancelled, the
Futurewill be cancelled,
- encounters an error, the
Future<T> is a generic class, where
T is the type of the value it can
For example, a
Future<String> instance can provide a
If the wrapped operation does not return anything, the type of the future is
In that case, if the operation is successful, the
Future<Void> will end
in a result state.
Future class provides the
get method that gives a synchronous
access to the value of the
get method blocks the current thread: this call is synchronous.
A synchronous call is blocking whereas an asynchronous one returns immediately.
If an error occurs in the wrapped operation, the
get call will throw an
ExecutionException and if the operation is cancelled, the
get call will
try-catch to determine in which state the
Future class contains several methods that allow you to chain several
asynchronous operations together:
then... methods allow you to chain 2 operations, regardless of the first
operation end state.
They are useful when you want to chain independent operations.
then... gives you access to the
Future wrapping the first operation.
isCancelled methods to determine the
state of the
andThen... methods allow you to chain 2 operations if and only if the first
one ends with a result state.
They are useful when some operation depends on the previous one in your chain.
andThen... directly gives you access to the value of the
wrapping the first operation:
andThen... as the boolean
& operator: the right side is not
evaluated if the left side is false.
Future wraps the operation equivalent to the sequence of the 2
We want Pepper to execute a dance made of several animations.
The animations must be chained together and we continue the dance even if one
animation fails: we use
We want Pepper to mimic an animal and then make someone guess this animal.
Pepper should listen to the answer if and only if the animation succeeds: we use
Depending on what your second operation returns, you will use one of the following methods:
...Consume to consume the
Future and return nothing:
...Apply to apply a transformation to the
Future’s value and return
a specific type:
...Compose to return a
This is mainly used to chain actions:
The operation represented by the lambda has different states depending on what is done inside this lambda:
|If lambda throws||Else|
Future is the one returned by the lambda when using
Each chaining operator takes a different callback as parameter:
Consumer interface contains the
It is used to consume the result of a
consume method of the
Consumer interface is executed
on a background thread.
Function interface contains the
It is used to return a new value /
execute method of the
Function interface is executed
on a background thread.