Skip to content

Instantly share code, notes, and snippets.

@superherointj
Created March 13, 2020 18:08
Show Gist options
  • Save superherointj/fab34e6963a09f215f42e987c0cc7bbf to your computer and use it in GitHub Desktop.
Save superherointj/fab34e6963a09f215f42e987c0cc7bbf to your computer and use it in GitHub Desktop.
Two LWT Promises meet and say Hello World *TOGETHER*
(executable
(name lwthelloworld)
(public_name lwthelloworld)
(libraries lwt lwt.unix)
(preprocess (pps lwt_ppx))
(flags (:standard -warn-error -22))
)
(*
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 *)
@superherointj
Copy link
Author

How can I have data retrieved from Lwt.t promise after Lwt.join completes despite Lwt.join expecting a list of unit Lwt.t?

@superherointj
Copy link
Author

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().

@superherointj
Copy link
Author

superherointj commented Mar 13, 2020

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