Last active
June 27, 2018 14:26
Star
You must be signed in to star a gist
F# Async Cache
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 cache (a:Async<'a>) : Async<'a> = | |
let tcs = TaskCompletionSource<'a>() | |
let state = ref 0 | |
async { | |
if (Interlocked.CompareExchange(state, 1, 0) = 0) then | |
Async.StartWithContinuations( | |
a, | |
tcs.SetResult, | |
tcs.SetException, | |
(fun _ -> tcs.SetCanceled())) | |
return! tcs.Task |> Async.AwaitTask } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment