Concurrent tasks#
promise(() => ...) starts concurrent work on a thread the runtime picks and returns a Promise[T]; wait() takes its result. Any function can wait, including functions that return a value directly, so there is no async or await.
Values that arrive over time travel on streams
, which are pulled lazily: ReadStream.next() asks its source for one item and waits when needed. An in-memory pipe connects promises.
import (
promise, wait_all from "@std/concurrency/promise.ms",
expect_equal from "@std/test/test.ms",
)
fn square(n Int) Int {
return n * n
}
test "run work at once and collect it" {
first := promise(() => ok(square(3)))
second := promise(() => ok(square(4)))
results := wait_all([first, second])?
expect_equal(results, [9, 16])?
}The test passes with the results in input order: [9, 16]. The two operations may finish in either order.
Starting, waiting and sharing#
- A
Promise[T]represents an operation with one result.wait()givesT!and takes the result once.promise(() => ...)starts work eagerly; stream reads may defer work until the wait. - A task pauses at a wait, including waits inside called functions. Other promises on the same scheduler thread run when it yields; promises on other threads can run at the same time.
promise(() => ...)runs on a thread the runtime picks and stays there once it starts. A promise nobody has started yet runs inside the task that waits for it, so a promise per call costs about a function call until another thread has idle time to take it. A CPU-heavy promise delays the promises sharing its thread until it waits. A promise may borrow what it captures, so the values it reads must outlive it (rule 4). A promise that a function returns owns its captures instead.- Work you start ends before the values it uses. Dropping a promise cancels it and waits for it to end.
- Concurrent code can read shared values but never change them. To change shared state, give it one owner and send it values over a
pipe. - Errors and faults inside a promise come back from
waitas errors. A fault outside any promise ends the program.
Use a pipe to transfer owned values between promises without serializing them. Each writer owns its stream end, and one reader owns the receiving end; the pipe example shows the lifetime of both.
The terminal (raw mode and key events in @std/io/io.ms) belongs to the main thread: a promise on another thread that uses it faults with “only available on the main thread”, so do terminal work in the main task and send it values.
| Page | What you will learn |
|---|---|
| Promises | Start work with a deadline, wait, cancel, and collect many results. |
| Streams and pipes | Read and write values over time, build pipelines, and pass values between promises. |
| HTTP server | Serve on every core with one serve call. |
For signatures and options, use the promise , I/O and network API pages.