Module Js_of_ocaml_eio.Eio_promise

Conversions between JavaScript promises (Js_of_ocaml.Promise) and Eio fibers.

exception Rejected of Js_of_ocaml.Promise.error

Raised by await when the promise is rejected. The rejection reason is wrapped unchanged, even when it is an OCaml exception (e.g. produced by make); use Js_of_ocaml.Promise.error_to_any to inspect it.

val await : ?abort:(unit -> unit) -> 'a Js_of_ocaml.Promise.t -> 'a

await ?abort p waits for the promise p to be settled and returns its value. Exception Rejected is raised if p is rejected. The optional abort function is called when the fiber is cancelled.

val make : sw:Eio.Switch.t -> (unit -> 'a) -> 'a Js_of_ocaml.Promise.t

make ~sw fn runs fn in a new fiber (attached to sw) and returns a promise which is resolved with the return value of the function. If the function raises an exception, the promise is rejected and sw is not failed. The rejection reason is e for exception Rejected e, the JavaScript error e for exception Js.Js_error.Exn e, and Promise.error_of_exn exn for any other exception exn. If sw is already cancelled or finished, the function is not run and the promise is rejected with the exception returned by Eio.Switch.get_error.

Every exception is turned into a rejection, including Eio.Cancel.Cancelled (when sw is cancelled while fn runs) and exceptions such as Assert_failure that indicate a bug: they are not reported elsewhere, so they are lost if the rejection is not handled.

val run : (unit -> 'a) -> 'a Js_of_ocaml.Promise.t

run fn runs fn in a new Eio context (Eio_js.start) and returns a promise of its result, rejected as with make if it raises. This is how JavaScript callbacks that must return a promise (e.g. request handlers) run Eio code: fn can use a Switch.run of its own. Unlike Eio_js.start, an exception raised by fn does not go to the uncaught exception handler.