Skip to content

Instantly share code, notes, and snippets.

@jeremyabbott
Created April 6, 2018 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyabbott/5d920e25df8208991f0410ffa86bff90 to your computer and use it in GitHub Desktop.
Save jeremyabbott/5d920e25df8208991f0410ffa86bff90 to your computer and use it in GitHub Desktop.
Async workflow to start something and come back to it later
let now () = System.DateTime.Now
let someOtherWork times = async {
let rec inner' times current = async {
if times = current then
printfn "someOtherWork FINISHED"
else
do! Async.Sleep 1000
let next = current + 1
printfn "someOtherWork step %d @ %s" next (now() |> string)
do! inner' times next
}
printfn "someOtherWork STARTED"
do! inner' times 0
}
// Async task that should start while we do other work.
// unit -> Async<string>
let childAsync () =
async {
do! Async.Sleep(5000)
return now()
}
// Async task to start the child and return a reference to it.
// Returns Async<Async<string>>
let returnAsync () =
async {
now()
|> string
|> printfn "LONG TASK STARTED @ %s"
//Async.StartChild returns Async<Async<T>>
// let! blocks on an async.
// a is Async<T> (T is a string)
let! a = childAsync() |> Async.StartChild
return a // returns Async<string>
} // whole async returns Async<Async<string>
// RunSynchronously unwraps Aysnc<T>
// Async<Async<T>> -> Async<T>
let longTask = returnAsync() |> Async.RunSynchronously
someOtherWork 10 |> Async.Start
longTask
|> Async.RunSynchronously
|> string
|> printfn "LONG TASK ENDED: %s"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment