package eio

  1. Overview
  2. Docs
On This Page
  1. Result promises
Effect-based direct-style IO API for OCaml

Install

dune-project
 Dependency

Authors

Maintainers

Sources

eio-1.4.tbz
sha256=ba11ad486f492130dbb486f2b3bdc4905643f10016806ddf86e9a34e4346aaa5
sha512=57ef2c137ccdc26d8029e636b6a4ee92da7c6b2f35954946bd56ca595d6947a8ec42f6f83f8552f73d6719e6ce2314cae2e2fad1686a387522935e6f90e9a8d9

doc/eio/Eio/Promise/index.html

Module Eio.PromiseSource

A 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)
Sourcetype !+'a t

An 'a t is a promise for a value of type 'a.

Sourcetype !-'a u

An 'a u is a resolver for a promise of type 'a.

Sourceval create : ?label:string -> unit -> 'a t * 'a u

create () is a fresh promise/resolver pair. The promise is initially unresolved.

Sourceval create_resolved : 'a -> 'a t

create_resolved x is a promise that is already resolved with result x.

Sourceval await : 'a t -> 'a

await t blocks until t is resolved. If t is already resolved then this returns immediately.

Sourceval resolve : 'a u -> 'a -> unit

resolve u v resolves u's promise with the value v. Any threads waiting for the result will be added to the run queue.

Sourceval try_resolve : 'a u -> 'a -> bool

try_resolve is like resolve but returns false instead of raising Invalid_argument.

Returns true on success.

Sourceval peek : 'a t -> 'a option

peek 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.

Sourceval is_resolved : 'a t -> bool

is_resolved t is Option.is_some (peek t).

Result promises

Sourcetype 'a or_exn = ('a, exn) result t
Sourceval resolve_ok : ('a, 'b) result u -> 'a -> unit

resolve_ok u x is resolve u (Ok x).

Sourceval resolve_error : ('a, 'b) result u -> 'b -> unit

resolve_error u x is resolve u (Error x).

Sourceval await_exn : 'a or_exn -> 'a

await_exn t is like await t, but if the result is Error ex then it raises ex.