-
-
Save superherointj/fab34e6963a09f215f42e987c0cc7bbf to your computer and use it in GitHub Desktop.
Two LWT Promises meet and say Hello World *TOGETHER*
This file contains hidden or 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
(executable | |
(name lwthelloworld) | |
(public_name lwthelloworld) | |
(libraries lwt lwt.unix) | |
(preprocess (pps lwt_ppx)) | |
(flags (:standard -warn-error -22)) | |
) |
This file contains hidden or 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
(* | |
The goal is to say "Hello World" with strings coming from the return of two different Lwt Promises. | |
Lwt.join waits for both promises to resolve but I don't know how to get data from Lwt Promises in this case | |
because Lwt.join expects a list of Lwt.t returning unit Lwt.t and not 'a Lwt.t | |
*) | |
let p_hello = Lwt.return () (* "Hello" *) | |
let p_world = Lwt.return () (* "World" *) | |
let p_main = | |
let%lwt mySentence = Lwt.join [p_hello; p_world] in | |
let%lwt () = Lwt_io.printl "Don't know greeting yet :(" in | |
Lwt.return () | |
let () = Lwt_main.run (p_main) | |
(* Thanks for the help *) |
I want to wait for p_hello and p_world to resolve, then I want to use the value return from both promises (would be string in this case, not unit). Very like JavaScript Promise.all().
Using Lwt.both worked just fine.
``
let p_hello = Lwt.return "Hello"
let p_world = Lwt.return "World"
let p_main =
let%lwt (a, b) = Lwt.both p_hello p_world in
Lwt_io.printl (a ^ " " ^ b)
let () = Lwt_main.run (p_main)
``
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can I have data retrieved from Lwt.t promise after Lwt.join completes despite Lwt.join expecting a list of unit Lwt.t?