← index

Asyncabstract

inherits Object

Structured concurrency over the task scheduler: run blocks as concurrent tasks whose I/O overlaps (gather:), park without blocking other tasks (sleep:), and put a deadline on anything (timeout:do:). See docs/internal/ASYNC_ARCH.md.

extended at core/05-async.qn:2

Async helpers layered over the native Async/Task scheduler primitives.

Class methods

gather:

Run a List of zero-parameter blocks as concurrent tasks -- their I/O overlaps -- and answer their results as a List in input order once all complete. Propagates the first error.

Async.gather:#( { Async.sleep:2; 'a' } { 'b' } )    "* -> #(a b)

native

joinAll:

Join a list of already-started Tasks concurrently, returning their values in the list's order. Unlike gather: (which spawns the work itself), this assumes each Task is already running — joining one parks the caller while the rest keep progressing on the scheduler, so the total wait is ~max(task), not the sum. A failed or cancelled task propagates its error out of the join.

var ts = #( { Async.sleep:1; 1 } { 2 } ).collect:{ |b| Task.spawn:b };
Async.joinAll:ts    "* -> #(1 2)

core/05-async.qn:14

sleep:Integer
sleep:Duration

Park the running task for the given milliseconds (a Duration is also accepted) without blocking other tasks; answers nil.

native

timeout:Integer do:Block
timeout:Duration do:Block

Run the block with a deadline of the given milliseconds (or a Duration): its value if it finishes in time; if the deadline fires first, the block is cancelled (its finally runs, in-flight I/O aborts) and a catchable timeout error raises.

Async.timeout:50 do:{ 42 }    "* -> 42

native

timeout:Integer do:Block onCancel:Block
timeout:Duration do:Block onCancel:Block

As timeout:do:, but when the deadline fires run the handler and answer ITS value instead of throwing (onCancel:{ nil } is the non-throwing form). The handler covers only this deadline: an outer cancellation still propagates and the handler does not run.

Async.timeout:5 do:{ Async.sleep:200; 1 } onCancel:{ 'late' }    "* -> late

native