Created
June 26, 2013 02:37
-
-
Save hcarty/5864329 to your computer and use it in GitHub Desktop.
Playing around with Lwt + something promise-y/future-y
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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