Skip to content

Instantly share code, notes, and snippets.

@ntreu14
Last active May 22, 2019 18:42
Show Gist options
  • Save ntreu14/507204927d15cd98f66fbaea30623ddb to your computer and use it in GitHub Desktop.
Save ntreu14/507204927d15cd98f66fbaea30623ddb to your computer and use it in GitHub Desktop.
open System
open System.Net
open System.Threading
[<EntryPoint>]
let main _ =
let url = "http://www.google.com"
let (>>=) (asyncBlock: Async<'a>) (f: 'a -> Async<'b>) =
async {
let! v = asyncBlock
return! f v
}
let (>=>) f g x =
f x >>= g
let getWebResponse () =
async {
let req = WebRequest.Create(Uri(url))
return! req.AsyncGetResponse ()
}
let readResponseAsString (response: WebResponse) =
async {
use stream = response.GetResponseStream()
use reader = new IO.StreamReader(stream)
return reader.ReadToEnd()
}
let f =
getWebResponse
>=> readResponseAsString
>=> fun s -> async { printfn "%s" s }
>> Async.Start
f ()
getWebResponse () >>= (fun _ -> async { printfn "Ignoring the web response and just printing." }) |> Async.Start
printfn "Where do I print?"
Thread.Sleep 1000000 // Don't kill the main program thread while we are doing the async stuff
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment