Future
Handle to an async result the host will eventually settle. Returned by every browser bridge — Browser.fetch, Browser.setTimeout, every Dom.* method, every WebSocket.* method. .await inside a fiber parks the current fiber until the future resolves.
The shape
| Member | Description |
|---|---|
.handle | Numeric handle the host uses to address this future. Implementation detail; useful only when bridging custom JS shims. |
.state | 0 pending, 1 resolved, 2 rejected. Useful for non-blocking polling. |
.await | Park the fiber until settled; return the resolved value or abort with the rejection message. |
Awaiting
Wrap host work in a fiber, then chain
.await on the futures the host gives you:
var fiber = Fiber.new {
var t = Browser.setTimeout(150).await
var html = Dom.text("#title").await
System.print(html)
}
fiber.try()
.await is implemented as a
Fiber.suspend() call against a scheduler the
runtime owns; calling .await outside a fiber
is a runtime error.
Non-blocking poll
Skip awaiting if you want to peek before yielding:
var f = Browser.fetch("…")
if (f.state == 1) {
System.print("already cached:", f.await)
} else {
// ...keep working, await later
}
ByteFuture
Same parking + scheduling shape as Future,
but .await resolves to a
ByteArray
instead of a String. Returned by
Browser.fetchBytes for raw asset loads —
images, audio, .hatch bundles, anything you'd
treat as binary.
var bytes = Browser.fetchBytes("./icon.png").await
System.print(bytes.count)
// Hand to a texture loader, audio decoder, etc.
Browser / DOM / WebSocket bridges
Every async browser bridge returns a Future:
| Bridge | Returns |
|---|---|
Browser.fetch(url) | Future<String> |
Browser.fetchBytes(url) | ByteFuture |
Browser.setTimeout(ms) | Future<null> |
Browser.nextFrame | Future<null> — resolves at the next requestAnimationFrame |
Dom.text(selector) · Dom.setText(s, v) · Dom.setHTML(s, html) · Dom.addClass(s, c) | Future<null> or Future<String> |
WebSocket.open(url) · .send(msg) · .receive | Future<…> per the call |
Worker mode and main-thread mode use the same shape;
worker mode round-trips through postMessage
under the hood. See
the WebAssembly
platform guide for the threading story.