NAME
std/task - task helpers for async ZuzuScript code.
SYNOPSIS
from std/task import all, race, sleep, yield, timeout, Channel, CancellationSource;
async function __main__ () {
let a := spawn { await { sleep(0.1); }; "a"; };
let b := spawn { await { sleep(0.1); }; "b"; };
return await { all( [ a, b ] ); };
}
IMPLEMENTATION SUPPORT
This module is supported by all implementations of ZuzuScript.
DESCRIPTION
This runtime-supported module provides the task type, cancellation objects, channels, timers, and combinators used with the await { ... } and spawn { ... } language forms.
The Phase A model follows JavaScript promises closely: creating a task starts independent work, and a spawned task's failure is observed only when the task is awaited or passed to a combinator such as all, race, or timeout. Unawaited spawned tasks are detached background work until the runtime shuts down. Async script entrypoints should be written as async function __main__ ( argv ) { ... }; the CLI awaits that function after loading the script.
EXPORTS
Classes
TaskThe runtime task type.
Tasks are awaitable values. Awaiting a fulfilled task returns its result. Awaiting a rejected task throws the rejection value. Awaiting a cancelled task throws
CancelledExceptionor the stored cancellation reason.task.status()Parameters: none. Returns:
String. Returnspending,running,sleeping,waiting,fulfilled,rejected, orcancelled.task.done()Parameters: none. Returns:
Boolean. Returns true when the task has fulfilled, rejected, or been cancelled.task.poll()Parameters: none. Returns:
Boolean. Makes one non-blocking progress check and returns whether the task has completed.task.cancel(reason?)Parameters:
reasonis an optional cancellation reason. Returns:Task. Cancels the task and returns the task.
ChannelConstruct with
new Channel(). Returns a simple channel object withsend,recv, andclose.sendreturns an immediately completed task for the buffered first version, or fails withChannelClosedExceptionif the channel is closed.recvreturns a pending task when no message is available and resolves tonullonce a closed channel has been drained.nullis the documented end-of-stream result for an empty closed channel.channel.send(value)Parameters:
valueis any value that may be sent through the channel. Returns:Task. Sends a value or fails if the channel is closed.channel.recv()Parameters: none. Returns:
Task. Resolves to the next value, or tonullafter a closed channel has drained.channel.close()Parameters: none. Returns:
null. Closes the channel.
CancellationTokenCancellation signal object.
token.cancelled()Parameters: none. Returns:
Boolean. Returns true when cancellation has been requested.token.reason()Parameters: none. Returns: value or
null. Returns the cancellation reason.token.throw_if_cancelled()Parameters: none. Returns:
null. Throws when cancellation has been requested.token.watch(task)Parameters:
taskis aTask. Returns:Task. Registerstaskfor cancellation when the token is cancelled.
CancellationSourceConstruct with
new CancellationSource(). Callingsource.cancel(reason)marks its token as cancelled, stores the cancellation reason, and cancels tasks registered withsource.token().watch(task).source.token()Parameters: none. Returns:
CancellationToken. Returns the source's token.source.cancel(reason?)Parameters:
reasonis an optional cancellation reason. Returns:null. Marks the token cancelled and cancels watched tasks.source.cancelled()Parameters: none. Returns:
Boolean. Returns true when the source has been cancelled.source.reason()Parameters: none. Returns: value or
null. Returns the stored cancellation reason.
Functions
resolved(value)Parameters:
valueis any value. Returns:Task. Returns a task that resolves tovalue.failed(message)Parameters:
messageis a failure value. Returns:Task. Returns a task that fails withmessage.sleep(seconds)Parameters:
secondsis a delay in seconds. Returns:Task. Returns a task that completes after the requested delay.yield()Parameters: none. Returns:
Task. Returns a task that completes after yielding back to the async scheduler once.all(tasks)Parameters:
tasksis an array of tasks. Returns:Task. Returns a task that awaits all tasks and resolves to an array of results in input order.If any input task rejects or is cancelled, the
alltask rejects or is cancelled with that same failure.race(tasks)Parameters:
tasksis an array of tasks. Returns:Task. Returns a task that resolves or fails with the first completed input task and cancels unfinished losing tasks.Loser cancellation is part of the public contract. Keep task handles and await them separately if later completion is important.
timeout(seconds, task)Parameters:
secondsis a timeout in seconds andtaskis the task to await. Returns:Task. Returns a task that fails withTimeoutExceptioniftaskdoes not complete before the timeout.
COPYRIGHT AND LICENCE
std/task is copyright Toby Inkster.
It is free software; you may redistribute it and/or modify it under the terms of either the Artistic License 1.0 or the GNU General Public License version 2.