Last active
June 29, 2017 18:16
-
-
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
This file contains 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
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