paradigm/async-await
Rust
Rust favoured async and await over other tecniques. It is a rather widespread algorithm.
async/.await are special pieces of Rust syntax that make it possible to yield control of the current thread rather than blocking, allowing other code to make progress while waiting on an operation to complete.
// `foo()` returns a type that implements `Future<Output = u8>`.
// `foo().await` will result in a value of type `u8`.
async fn foo() -> u8 { 5 }
fn bar() -> impl Future<Output = u8> {
// This `async` block results in a type that implements
// `Future<Output = u8>`.
async {
let x: u8 = foo().await;
x + 5
}
}
Colors
An async function is a red function
. A normal function is a blue function
. Red functions
are like parts of a system: they can wait for a result. It is like the Actor model or other system run-time paradigm. Red functions
can use other blue functions
or they can call other red functions
, but a blue function
cannot call a red function
, because doing so, it will not be anymore a “pure function” calculating the result of an algo, but part of the system.