Skip to content

Instantly share code, notes, and snippets.

@hcarty
Created June 26, 2013 02:37
Show Gist options
  • Save hcarty/5864329 to your computer and use it in GitHub Desktop.
Save hcarty/5864329 to your computer and use it in GitHub Desktop.
Playing around with Lwt + something promise-y/future-y
let ( >>= ) = Lwt.( >>= )
let promise f x =
let mbox = Lwt_mvar.create_empty () in
Lwt.async (
fun () ->
f x >>= fun y ->
Lwt_mvar.put mbox y
);
mbox
let force mbox =
Lwt_mvar.take mbox
let main =
let timer = promise Lwt_unix.sleep 20.0 in
Lwt_io.printlf "Hello!">>= fun () ->
Lwt_io.printl "Now I'm going to wait!" >>= fun () ->
force timer >>= fun () ->
Lwt_io.printlf "Yay! All done!"
let () =
Lwt_main.run main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment