@std/concurrency/promise.ms#
Import each required symbol with import Name from "@std/concurrency/promise.ms".
Cancel Type#
A switch that cancels every promise made with its signal(). cancel() never
waits; the work stops at its next wait and wait reports ECANCELED. Dropping the
switch does not cancel anything: promises made with its signals keep running.
opaque type Cancel {
}Cancel.cancel#
Cancels every promise made with a signal of this switch, now and later. Calling it again is safe.
fn cancel() VoidCancel.close#
Frees the switch. Promises made with its signals keep running, but the switch can no longer cancel them.
move fn close() Result[Void, Error]Cancel.signal#
Returns a handle for PromiseOptions(cancel: ...). Make as many as you need.
fn signal() SignalPlacement Enum#
Which thread runs a promise’s work.
enum Placement {
Any
Spread
Here
}Placement.Any#
A thread the runtime picks: queued on the calling thread for an idle one to take.
AnyPlacement.Here#
A task on the calling thread. Use it for work that uses values bound to this thread (a network handle, a connection, a socket) and only waits (on a file, a socket, a child process). Work that computes blocks the thread.
HerePlacement.Spread#
The least loaded thread at once, starting a thread when the limit allows. For long-lived work that should use every core, such as one serving worker per core.
SpreadPromise Type#
An operation with one result. wait takes the result once, and a stream read may
defer its work until the wait. Dropping an unfinished promise cancels it and waits
for it to end. A promise that is never used or waited on is a warning.
opaque type Promise[T] {
}Promise.cancel#
Asks the work to stop at its next wait; wait then reports ECANCELED. It
returns at once, and dropping the promise waits for the work to end. A finished
promise is unchanged.
fn cancel() VoidPromise.close#
Dropping a promise ends it: unfinished work is cancelled and waited for, and a result nobody took is dropped, which closes what it holds.
move fn close() Result[Void, Error]Promise.done#
Reports whether the work has finished, however it ended. It never waits.
fn done() BoolPromise.wait#
Waits for the result and moves it out. It can return:
- the operation’s own error;
EIO, when the work faulted (the report is indetails["report"]);ECANCELED, when the work was cancelled;EBADF, when an earlier wait already took the result;ETIMEDOUT, once the promise’stimeoutoption has passed, which also cancelled the work.
fn wait() Result[T, Error]Promise.with#
Applies options to a promise made elsewhere (read_file, fetch, sleep,
exec) and returns it, so
read_file(path).with(options: PromiseOptions(timeout: 5.seconds())).wait() bounds
the read. The timeout counts from now. A provided cancel signal replaces the previous
one.
fn with(options PromiseOptions = PromiseOptions()) Promise[T]PromiseOptions Type#
What bounds a promise, fixed when it is made. timeout counts from construction and
is enforced whether or not anyone waits; when it passes, the work is cancelled and
wait reports ETIMEDOUT. A negative timeout means no time at all.
type PromiseOptions {
timeout Duration? = null
cancel Signal? = null
placement Placement = Placement.Any
}PromiseOptions.cancel#
When this signal’s Cancel fires, the work is cancelled and wait reports
ECANCELED. Applying a signal to an existing promise replaces its previous
signal; null leaves it unchanged.
cancel Signal? = nullPromiseOptions.placement#
Which thread runs the work.
placement Placement = Placement.AnyPromiseOptions.timeout#
How long the work may run, counted from construction; null means no limit.
timeout Duration? = nullSignal Type#
A copyable handle to a Cancel, passed in PromiseOptions. Once its Cancel has
fired, it reads as cancelled for good, even after the Cancel is closed or dropped.
A Cancel that never fired reads as not cancelled once it is gone.
opaque type Signal {
}Signal.cancelled#
Reports whether the switch has fired. Use it in work that loops without waiting.
fn cancelled() Booladopt Function#
Wraps the runtime promise id, which a native operation completes (a stream read
or flush), as a Promise.
fn adopt[U](id Int) Promise[U] { ... }caller_frame Function#
Returns where the call that omitted this argument is, as a fault report shows a
frame: file:line in function. Use it only as a parameter default, in a function
that is called directly; the compiler fills it in as a constant at each call.
fn caller_frame() String { ... }cancel_switch Function#
Returns a new switch that has not fired.
fn cancel_switch() Cancel { ... }complete Function#
Stores how the work ended (its result or error) in the promise id.
fn complete[U](id Int, move result Result[U, Error]) Void { ... }deferred Function#
Returns a promise whose work runs in the task that first waits on it, on that task’s
stack, with no task of its own. The work may wait itself. work receives the new
promise’s id and must finish with complete(id, ...). This is for stream
implementations. Dropping the promise before any wait discards the work unrun.
fn deferred[U](work fn(Int) Void) Promise[U] { ... }promise Function#
Runs entry as a promise and returns the promise. The closure follows the capture
rules. A promise that is returned owns what entry captures and closes or drops it
when its work ends (bind a .clone() first to keep a value). A promise that stays in
the function reads its captures in place, so they must outlive it and cannot change
while it lives; to give it a resource, move the resource into a helper that returns
the promise. A fault
inside ends only this promise, and wait reports it as EIO. options.placement
picks the thread. site is the line a fault report gives after task started by:;
leave it out and it is the line of this call. A helper that starts a promise for its
caller takes site: String = caller_frame() itself and passes it on.
fn promise[U](entry fn() Result[U, Error], options PromiseOptions = PromiseOptions(), site String = caller_frame()) Promise[U] { ... }promise_when_idle Function#
Starts entry like a Placement.Here promise, except that when no other task is
running it runs in the task that first waits on it, which saves the cost of a task
of its own. Use it for operations that do not block for long, such as reading a
file. The work follows the capture rules of promise.
fn promise_when_idle[U](entry fn() Result[U, Error], site String = caller_frame()) Promise[U] { ... }set_options Function#
Applies options to a promise in place, for an owner that keeps the promise (such
as a server’s accept loop). The timeout counts from now, and a provided cancel
signal replaces the previous one. with is the chaining form.
fn set_options[T](target Promise[T], options PromiseOptions = PromiseOptions()) Void { ... }wait_all Function#
Waits for every promise and returns the results in order. The first promise to fail,
in time rather than list order, ends the wait: the promises still running are
cancelled and waited for before wait_all returns that error.
fn wait_all[T](move promises List[Promise[T]]) Result[List[T], Error] { ... }wait_first Function#
Waits for the first promise to finish and returns its result; the others are
cancelled and waited for. Errors are those of wait, and an empty list is an
error.
fn wait_first[T](move promises List[Promise[T]]) Result[T, Error] { ... }