Using LWT

Run with iocaml -js full <notebook>

Lwt in the browser. The code doesn't lock up the browser while it's running.


In [1]:
let rec f i = 
    if i=10 then Lwt.return ()
    else
        Lwt.(return (Iocaml.send_clear ~stdout:false (); 
                     Printf.printf "hello stdout %i\n" i;
                     Printf.fprintf stderr "hello stderr %i\n" i)
        >>= fun () -> Lwt_js.sleep 0.5
        >>= fun () -> f (i+1))


Out[1]:
val f : int -> unit Lwt.t = <fun>

In [2]:
f 0


hello stdout 0
Out[2]:
- : unit Lwt.t = <abstr>
hello stdout 1
hello stdout 2
hello stdout 3
hello stdout 4
hello stdout 5
hello stdout 6
hello stdout 7
hello stdout 8
hello stdout 9
hello stderr 9