Skip to content

Instantly share code, notes, and snippets.

@jbrestan
Last active June 29, 2017 18:16
Show Gist options
  • Save jbrestan/a5c9a59a2f40d45ea4bd04877abd45ff to your computer and use it in GitHub Desktop.
Save jbrestan/a5c9a59a2f40d45ea4bd04877abd45ff to your computer and use it in GitHub Desktop.
Snippet showing how `Async<'t>` always gets fully executed, so there's nothing like "completed Async instance". Took me a while to understand why there are no `Async.IsCompleted` or `Async.Result` properties like on Task
let rec retryForever (delay: TimeSpan) (workflow: Async<Result<_, _>>) = async {
(*
this always fully executes the `workflow` again,
no result caching like Task's Result property
*)
let! result = workflow
match result with
| Ok r ->
return Ok r
| Error _ ->
do! Async.Sleep(int delay.TotalMilliseconds)
return! retryForever delay workflow
}
let alwaysFail: Async<Result<unit, unit>> = async {
printfn "Running"
return Error ()
}
retryForever (TimeSpan.FromSeconds(1.)) alwaysFail |> Async.RunSynchronously
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment