Module Async.Call_handler

Call handlers.

A call handler wraps an asynchronous function call f before it starts executing. It is responsible for making the f () call. The handler is also used to make all the asynchronous calls done by f itself and its descendents.

The composition order of handlers matches the syntactic scope: outer scopes are called after inner ones. More precisely in the example below f is called with (Call_handler.compose h0 h1).handle f:

let h0 = Fun.Async.Call_Handler.{ handle = … }
let h1 = Fun.Async.Call_Handler.{ handle = … }
let ret =
  Fun.Async.call ~handler:h0 @@ fun () ->
  Fun.Async.call ~handler:h1 @@
  f (* Formally: h0.handle (fun () -> h1.handle f) *)

Handlers can be used to make sure effects are handled in asynchronous function calls according to expected syntactic scopes, see the cookbook.

type t = {
  1. handle : 'a. (unit -> 'a) -> 'a;
}

The type for handlers.

Given a function f, handle must call f () and returns its value or exception.

Warning. handle must be synchronization safe. It can be executed in parallel and/or on multiple different domains over time.

val call : t -> (unit -> 'a) -> 'a

call h f is h.handle f.

val none : t

none is a handler that just executes the function.

val compose : t -> t -> t

(compose h0 h1).handle f is h0.handle (fun () -> h1.handle f).

val of_list : t list -> t

of_list hs is List.fold_right compose hs f none.