Guest User

Untitled

a guest
Jun 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. let withCancellation (ct:CancellationToken) (a:Async<'a>) : Async<'a> = async {
  2. let! ct2 = Async.CancellationToken
  3. use cts = CancellationTokenSource.CreateLinkedTokenSource (ct, ct2)
  4. let tcs = new TaskCompletionSource<'a>()
  5. use _reg = cts.Token.Register (fun () -> tcs.TrySetCanceled() |> ignore)
  6. let a = async {
  7. try
  8. let! a = a
  9. tcs.TrySetResult a |> ignore
  10. with ex ->
  11. tcs.TrySetException ex |> ignore }
  12. Async.Start (a, cts.Token)
  13. return! tcs.Task |> Async.AwaitTask }
Add Comment
Please, Sign In to add comment