Async Expression
async
expressions are a special kind of expression that can take any expression or single statement and cause it to be evaluated or performed asynchronously, without blocking the execution flow in the place the async
statement is being called. Only when the value of an async
expression is being explicitly accessed at a later time will the execution flow block and wait for the value to become available, if necessary.
To represent that their value will not be available immediately, async
expressions will always be of a Future type matching the type of the inner expression.
For example, if the inner expression would be of type Integer
, the corresponding async expression will be of type future Integer
, instead.
var len1 := async SlowlyCountCharactersInString("2nd String"); // len2 will be a future Integer
var len2 := SlowlyCountCharactersInString("Some String"); // len will be a regular Integer
var l := len1+len2; // waits for the len1 Future to finish evaluating, if necessary
Plain Statements in async
expressions
Despite being expressions themselves, async
expressions can also work on a plain statement, which has no resulting value of its own. In such a case, the async expression
will be a type-less Future, also sometimes referred to as a Void Future.
Unlike typed futures, a typeless future has no value that it represents, but it can be used to wait for the asynchronous task that it represents to be finished.
var fut := async begin // goes off and does a couple of things in the background
DoSomething();
DoSomethingElse();
end;
DoSomeMore(); // meanwhile, do more work on the current thread
fut(); // wait for the type-less future to finish, if it hasn't already.
var x := fut(); // ERROR. fut has no value.
You can and should read more about Futures, both typed and type-less, here