Fiber
Cooperative coroutines. Fiber.new {} creates a suspended one; .call runs it until it yields, returns, or aborts. .try is the same as .call but returns rejections instead of propagating them.
Construct & identity
| Method | What it does |
|---|---|
Fiber.new(body) | Wrap a closure as a new suspended fiber. |
Fiber.current | The fiber that's currently running. |
.isDone | true when the fiber has returned (or aborted). |
var f = Fiber.new {
System.print("inside")
}
f.call() // inside
f.isDone // true
Yield & resume
| Method | What it does |
|---|---|
.call() · .call(value) | Resume the fiber. Returns the value the fiber yielded or returned. |
Fiber.yield() · Fiber.yield(v) | Yield to the caller. Stays alive; the caller's .call returns v (or null). |
Fiber.suspend() | Suspend the running fiber's enclosing transfer (rarely needed directly; used by async bridges). |
Fiber.abort(error) | Abort the fiber. Up the stack, .try { ... } catches it as the fiber's .error slot. |
var counter = Fiber.new {
var i = 0
while (true) {
Fiber.yield(i)
i = i + 1
}
}
System.print(counter.call()) // 0
System.print(counter.call()) // 1
System.print(counter.call()) // 2
.call vs .try
.call propagates aborts up the parent stack.
.try swallows them and returns the
error value instead, so the parent can decide
how to react.
var fragile = Fiber.new { Fiber.abort("nope") }
var r = fragile.try()
System.print(fragile.error) // "nope"
System.print(r) // null
Cancellation & deadlines
| Method | What it does |
|---|---|
Fiber.isCancelled | true when an external caller has requested cancellation. |
Fiber.cancel() | Mark this fiber for cancellation. Compute loops can poll Fiber.isCancelled and bail. |
Fiber.deadlineMs | Deadline in milliseconds (host-monotonic), or null if none set. |
Fiber.setDeadlineMs(ms) | Set the deadline. Runtime checkpoints inside loops poll this; expiring deadlines surface as aborts. |
var work = Fiber.new {
while (!Fiber.isCancelled) {
step()
}
}
work.call()
Async-bridge integration
Browser bridges — Browser.fetch,
Browser.setTimeout, Dom.*,
WebSocket.* — return a Future.
Calling .await on a future inside a fiber
suspends it through Fiber.suspend; the host
scheduler resumes it via .call(value) when
the future settles.
var page = Fiber.new {
var html = Browser.fetch("https://example.com").await
System.print(html.count)
}
page.try()
See Future for the
settled-value shape and the difference between
Future and ByteFuture.