9.5. Async/await coroutine macros

The ASYNC_BOOST module implements an async/await pattern for daslang using generator-based cooperative multitasking. It provides the [async] function annotation, await for waiting on results, and await_next_frame for suspending until the next step. Under the hood every [async] function is transformed into a state-machine generator — no threads, channels, or job queues are involved.

All functions and symbols are in “async_boost” module, use require to get access to it.

require daslib/async_boost

See also

Async / Await — Tutorial 49: Async / Await.

Coroutines and additional generator support — coroutines module (underlying generator framework).

9.5.1. Function annotations

async_boost::AwaitMacro

Function annotation that implements coroutine await semantics.

async_boost::AwaitCoroutineMacro

This macro converts await(<coroutine>) expression into:

for t in THAT
    yield t

The idea is that coroutine or generator can continuously yield from another sub-coroutine or generator.

async_boost::async

This macro converts function into generator. Generator yields bool if its a void function (coroutine), and yields the return type otherwise (async return). async function can wait for another async function using await(<async fn call>). use ‘return false’ to immediately return from the generator.

9.5.2. Awaiting

9.5.2.1. await

async_boost::await(a: iterator<bool>) : bool()

This function is used to wait for the result of the async function.

Arguments
  • a : iterator<bool>

async_boost::await(a: iterator<variant<res:auto(T);wait:bool>>) : T()

async_boost::await_next_frame()

This function is used to suspend coroutine until next frame.

9.5.3. Running async tasks

async_boost::async_run(a: iterator<auto>) : auto()

This function runs async function until it is finished.

Arguments
  • a : iterator<auto>

async_boost::async_run_all(a: array<iterator<auto>>) : auto()

This function runs all async function until they are finished (in parallel, starting from the last one).

Arguments
  • a : array<iterator<auto>>

9.5.4. Uncategorized

async_boost::async_timeout(a: iterator<auto>; max_frames: int) : bool()

This function runs an async function for at most max_frames frames. Returns true if the async function completed within the limit, false if it was terminated due to timeout.

Arguments
  • a : iterator<auto>

  • max_frames : int

async_boost::async_race(a: iterator<auto>; b: iterator<auto>) : int()

This function runs two async functions concurrently and returns the index (0 or 1) of whichever finishes first. The other is abandoned.

Arguments
  • a : iterator<auto>

  • b : iterator<auto>