Eio.PromiseA promise is a placeholder for result that will arrive in the future.
Unlike lazy values, you cannot "force" promises; a promise is resolved when the maker of the promise is ready.
Promises are thread-safe and so can be shared between domains and used to communicate between them.
Example:
let promise, resolver = Promise.create () in
Fiber.both
(fun () -> traceln "Got %d" (Promise.await promise))
(fun () -> Promise.resolve resolver 42)create () is a fresh promise/resolver pair. The promise is initially unresolved.
val create_resolved : 'a -> 'a tcreate_resolved x is a promise that is already resolved with result x.
val await : 'a t -> 'aawait t blocks until t is resolved. If t is already resolved then this returns immediately.
val resolve : 'a u -> 'a -> unitresolve u v resolves u's promise with the value v. Any threads waiting for the result will be added to the run queue.
val try_resolve : 'a u -> 'a -> booltry_resolve is like resolve but returns false instead of raising Invalid_argument.
Returns true on success.
val peek : 'a t -> 'a optionpeek t is Some v if the promise has been resolved to v, or None otherwise. If the result is None then it may change in future, otherwise it won't. If another domain has access to the resolver then the state may have already changed by the time this call returns.
val is_resolved : 'a t -> boolis_resolved t is Option.is_some (peek t).
type 'a or_exn = ('a, exn) Stdlib.result tval resolve_ok : ('a, 'b) Stdlib.result u -> 'a -> unitresolve_ok u x is resolve u (Ok x).
val resolve_error : ('a, 'b) Stdlib.result u -> 'b -> unitresolve_error u x is resolve u (Error x).
val await_exn : 'a or_exn -> 'aawait_exn t is like await t, but if the result is Error ex then it raises ex.