Js_of_ocaml.PromiseBindings to the JavaScript Promise API.
A value of type 'a t represents a JavaScript promise that, when fulfilled, resolves with a value of OCaml type 'a.
Native JavaScript promises automatically flatten any thenable returned from a handler or passed to Promise.resolve. That means a JavaScript Promise can never resolve with another Promise as its value, which is unsound at the OCaml type level for, say, 'a t t.
These bindings work around that by wrapping thenable values in a small container before resolving and unwrapping on the way out; non-thenable values are passed through unchanged so the common case pays no allocation. As a result, 'a t values always resolve with a value of OCaml type 'a, even when 'a is itself '_ t.
Use of_any / to_any to interoperate with promises coming from outside this module. A 'a t obtained via of_any from a foreign promise will not be wrapped, so it is up to the caller to ensure the types line up.
The reason a promise was rejected. JavaScript allows rejecting with any value (not necessarily an Error), so error is opaque; use error_to_any to inspect it and error_of_any to construct one.
val error_of_any : Js.Unsafe.any -> errorval error_to_any : error -> Js.Unsafe.anyval error_of_exn : exn -> errorUse an OCaml exception as a rejection reason.
val resolve : 'a -> 'a tA promise already fulfilled with the given value.
make f runs f synchronously with two callbacks; f is expected to eventually invoke either resolve or reject to settle the promise.
with_resolvers () returns (p, resolve, reject) where p is a fresh promise that is settled by calling resolve x (to fulfill with x) or reject e (to reject with e). Bound to the Promise.withResolvers() static method (ES2024).
then_ f p returns a new promise that, when p fulfills with x, fulfills (or rejects) like f x. If p is rejected, the rejection is propagated.
If ~on_error is supplied, this is the two-callback form of .then(f, g) in JavaScript: on_error only fires for rejections of p itself. If f returns a rejected promise, on_error does not catch it; chain a catch afterwards if you need that.
catch f p returns a new promise that, when p is rejected with reason e, fulfills (or rejects) like f e. If p is fulfilled, its value is propagated.
finally f p returns a new promise that settles the same way as p, after invoking f for its side effect.
all ps resolves with the values of all promises in ps, in order, or rejects with the reason of the first promise to reject.
all_settled ps resolves once every promise in ps has settled, with a list in the same order: Ok v for a fulfilled promise and Error e for a rejected one. The returned promise never rejects.
any ps fulfills as soon as any promise in ps fulfills. If every promise rejects, the returned promise rejects with an AggregateError whose errors property lists the rejection reasons.
race ps settles like the first promise in ps to settle, fulfilled or rejected.
val to_any : 'a t -> Js.Unsafe.anyval of_any : Js.Unsafe.any -> 'a tTreat a foreign JavaScript promise as a 'a t. The returned value is only sound if the underlying promise actually resolves with a value of type 'a; raw foreign values are passed through then_ unchanged.