A value that will become determined asynchronously.

A deferred can be "undetermined" or "determined". A deferred that is undetermined may at some point become determined with value v, and will henceforth always be determined with value v.

`include sig ... end`

`val sexp_of_t : ('a ‑> Base.Sexp.t) ‑> 'a t ‑> Base.Sexp.t`

`include Core_kernel.Invariant.S1 with type a t := a t`

`val invariant : 'a Base__.Invariant_intf.inv ‑> 'a t Base__.Invariant_intf.inv`

`sexp_of_t t f`

returns a sexp of the deferred's value, if it is determined, or an
informative string otherwise.

This is just for display purposes. There is no `t_of_sexp`

.

`create f`

calls `f i`

, where `i`

is an empty ivar. `create`

returns a deferred that
becomes determined when `f`

fills `i`

.

`val upon : 'a t ‑> ('a ‑> unit) ‑> unit`

`upon t f`

will run `f v`

at some point after `t`

becomes determined with value
`v`

.

`val value_exn : 'a t ‑> 'a`

`value_exn t`

returns `v`

if `t`

is determined with value `v`

, and raises
otherwise.

Deferreds form a monad.

`let%bind v = t in f v`

returns a deferred `t'`

that waits until `t`

is determined
with value `v`

, at which point it waits for `f v`

to become determined with value
`v'`

, to which `t'`

will become determined.

`return v`

returns a deferred that is immediately determined with value v.

Note that:

`upon t f`

is more efficient than:

`ignore (let%bind a = t in f a; return ())`

because `upon`

, unlike `let%bind`

, does not create a deferred to hold the result.

For example, one can write a loop that has good constant factors with:

```
let rec loop () =
upon t (fun a -> ... loop () ... )
```

although often `forever`

or `repeat_until_finished`

is more clear.

The same loop written with `let%bind`

would allocate deferreds that would be
immediately garbage collected. (In the past, this loop would have also used linear
space in recursion depth!)

In general, for deferreds that are allocated by `let%bind`

to be garbage collected
quickly, it is sufficient that the allocating bind be executed in tail-call position
of the right-hand side of an outer bind.

`include Core_kernel.Monad with type a t := a t`

`include Base__.Monad_intf.S_without_syntax with type a t := a t`

`include Base__.Monad_intf.Infix with type a t := a t`

`module Monad_infix : Base__.Monad_intf.Infix with type a t := a t`

`module Infix : sig ... end`

`all ts`

returns a deferred that becomes determined when every `t`

in `t`

s is
determined. The output is in the same order as the input.

`val don't_wait_for : unit t ‑> unit`

`don't_wait_for t`

ignores `t`

. It is like `Fn.ignore`

, but is more constrained
because it requires a `unit Deferred.t`

.

Rather than `ignore (t : _ t)`

, do `don't_wait_for (Deferred.ignore t)`

.

We chose to give `don't_wait_for`

type `unit t`

rather than `_ t`

to catch errors
where a value is accidentally ignored.

`module Choice : sig ... end`

A `Choice.t`

is used to produce an argument to `enabled`

or `choose`

. See below.

`enabled [choice t1 f1; ... choice tn fn;]`

returns a deferred `d`

that becomes
determined when any of the `ti`

becomes determined. The value of `d`

is a function
`f`

that when called, for each `ti`

that is enabled, applies `fi`

to `ti`

, and returns
a list of the results. It is guaranteed that the list is in the same order as the
choices supplied to `enabled`

, but of course it may be shorter than the input list if
not all `ti`

are determined.

```
choose [ choice t1 f1
; ...
; choice tn fn ]
```

returns a deferred `t`

that becomes determined with value `fi ai`

after some `ti`

becomes determined with value `ai`

. It is guaranteed that `choose`

calls at most one
of the `fi`

s, the one that determines its result. There is no guarantee
that the `ti`

that becomes determined earliest in time will be the one whose value
determines the `choose`

. Nor is it guaranteed that the value in `t`

is the first value
(in place order) from `choices`

that is determined at the time `t`

is examined.

For example, in:

```
choose [ choice t1 (fun () -> `X1)
; choice t2 (fun () -> `X2) ]
>>> function
| `X1 -> e1
| `X2 -> e2
```

it may be the case that both `t1`

and `t2`

become determined, yet `e2`

actually runs.

It is guaranteed that if multiple choices are determined with no intervening
asynchrony, then the earliest choice in the list will become the value of the
`choose`

.

`val repeat_until_finished : 'state ‑> ('state ‑> [ `Repeat of 'state | `Finished of 'result ] t) ‑> 'result t`

`repeat_until_finished initial_state f`

repeatedly runs `f`

until `f`

returns
``Finished`

. The first call to `f`

happens immediately when `repeat_until_finished`

is called.

`val forever : 'state ‑> ('state ‑> 'state t) ‑> unit`

`forever initial_state f`

repeatedly runs `f`

, supplying the state returned to the
next call to `f`

.

`val ok : 'a t ‑> ('a, _) Core_kernel.Result.t t`

Useful for lifting values from the `Deferred.t`

monad to the `Result.t Deferred.t`

monad.

These contain operations for iterating in a deferred manner over different collection types.

`module Array = Async_kernel__.Deferred_array`

`module List = Async_kernel__.Deferred_list`

`module Map = Async_kernel__.Deferred_map`

`module Memo = Async_kernel__.Deferred_memo`

`module Queue = Async_kernel__.Deferred_queue`

`module Sequence = Async_kernel__.Deferred_sequence`

These contain interfaces for working with deferred type containing error-aware types,
like `'a Option.t Deferred.t`

, or `'a Or_error.t Deferred.t`

. These all include
support for monadic programming.

`module Option = Async_kernel__.Deferred_option`

`module Or_error = Async_kernel__.Deferred_or_error`

`module Result = Async_kernel__.Deferred_result`