Skip to content

Commit 2b56bc6

Browse files
committed
some docs
1 parent 7145d61 commit 2b56bc6

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/direct/lwt_direct.mli

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,39 @@
1-
(** Direct style control flow for Lwt. *)
1+
(** Direct style control flow for Lwt.
2+
3+
This module relies on OCaml 5's
4+
{{:https://ocaml.org/manual/5.3/effects.html} effect handlers}.
5+
Instead of chaining promises using {!Lwt.bind} and {!Lwt.map}
6+
and other combinators, it becomes possible to start
7+
lightweight "tasks" using [Lwt_direct.run (fun () -> ...)].
8+
The body of such a task is written in direct-style code,
9+
using OCaml's standard control flow structures such as loops,
10+
higher-order functions, exception handlers, [match], etc.
11+
12+
Interactions with the rest of lwt can be done using [await],
13+
for example:
14+
15+
{[
16+
Lwt_direct.run (fun () ->
17+
let continue = ref true in
18+
while !continue do
19+
match Lwt_io.read_line in_channel |> Lwt_direct.await with
20+
| exception End_of_file -> continue := false
21+
| line ->
22+
let uppercase_line = String.uppercase_ascii line in
23+
Lwt_io.write_line out_channel |> Lwt_direct.await
24+
done)
25+
]}
26+
27+
This code snippet contains a simple "task" that repeatedly reads
28+
a line from a [Lwt_io] channel, uppercases it, and writes the
29+
uppercase version to another channel.
30+
31+
This task is itself a [unit Lwt.t], which is resolved when the function
32+
returns. It is possible to use
33+
{!Lwt_direct.run_in_the_background} to ignore the result and
34+
let the task run in the background instead.
35+
36+
*)
237

338
val run : (unit -> 'a) -> 'a Lwt.t
439
(** [run f] runs the function [f ()] in a task within
@@ -31,5 +66,5 @@ val await : 'a Lwt.t -> 'a
3166
exception with which [prom] failed if it failed.
3267
If [prom] is not resolved yet, [await prom] will suspend the
3368
current task and resume it when [prom] is resolved.
34-
calling [yield] outside of {!run} or {!run_in_the_background} will raise an exception,
69+
calling [await] outside of {!run} or {!run_in_the_background} will raise an exception,
3570
crash your program, or otherwise cause errors. It is a programming error to do so. *)

0 commit comments

Comments
 (0)