Module Fut
Future values.
A future 'a Fut.t
is an undetermined value of type 'a
that becomes determined at an arbitrary point in the future. The future acts as a placeholder for the value while it is undetermined.
Brr
uses future values ('a, 'b) result Fut.t
to type the resolution and rejection case of JavaScript promises. Since most rejection cases given by browser APIs are simply Jv.Error.t
values, the dedicated Fut.or_error
type alias can be used for that.
Fut.t
values are indirectly implemented as Promise objects that never reject. You can't substitute them directly for JavaScript promises and vice-versa, use of_promise
and to_promise
to convert between them.
Futures
val create : unit -> 'a t * ('a -> unit)
create ()
is(f, set
withf
the future value andset
the function toset
it. The latter can be called only once, aJv.Error
is thrown otherwise.
val await : 'a t -> ('a -> unit) -> unit
await f k
waits forf
to determinev
and continues withk v
. If the future never determinesk
is not invoked.k
must not raise.
val return : 'a -> 'a t
return v
is a future that determinesv
.
val bind : 'a t -> ('a -> 'b t) -> 'b t
bind f fn
is the futurefn v
withv
the value determined byf
.
val pair : 'a t -> 'b t -> ('a * 'b) t
pair f0 f1
is the future that determines with the value off0
andf1
.
val of_list : 'a t list -> 'a list t
of_list fs
determines with the values of all futurefs
in the same order.
val tick : ms:int -> unit t
tick ~ms
determines()
ms
milliseconds after creation usingsetTimeout
.
Future results
type nonrec ('a, 'b) result
= ('a, 'b) Stdlib.result t
The type for future values that may error.
type 'a or_error
= ('a, Jv.Error.t) result
The type for future values that error with a JavaScript error.
val ok : 'a -> ('a, 'b) result
ok v
isreturn (Ok v)
.
val error : 'b -> ('a, 'b) result
error e
isreturn (Error e)
.
Converting with JavaScript promises
val of_promise : ok:(Jv.t -> 'a) -> Jv.Promise.t -> 'a or_error
of_promise ~ok p
isof_promise' ~ok ~error:Jv.to_error. p
.
val to_promise : ok:('a -> Jv.t) -> 'a or_error -> Jv.Promise.t
to_promise p
isto_promise' ~ok ~error:Jv.of_error p
.
val of_promise' : ok:(Jv.t -> 'a) -> error:(Jv.t -> 'b) -> Jv.Promise.t -> ('a, 'b) result
of_promise ~ok ~error p
is a future for the promisep
. The future determines withOk (ok v)
ifp
resolves withv
and withError (error e)
ifp
rejects withe
.
val to_promise' : ok:('a -> Jv.t) -> error:('b -> Jv.t) -> ('a, 'b) result -> Jv.Promise.t
to_promise f
is a JavaScript promise for the futuref
that resolves the promise withok v
if the future determines withOk v
and rejects withe
if the future determines withError e
.
Future syntaxes
module Syntax : sig ... end
Future syntax.
module Result_syntax : sig ... end
Future result syntax.