Skip to content

Commit 2f8829b

Browse files
committed
Fix ValueTask.ignore
1 parent 3e3d926 commit 2f8829b

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/FSharpPlus/Extensions/ValueTask.fs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,20 @@ module ValueTask =
226226

227227
/// <summary>Creates a ValueTask that ignores the result of the source ValueTask.</summary>
228228
/// <remarks>It can be used to convert non-generic ValueTask to unit ValueTask.</remarks>
229-
let ignore (source: ValueTask<'T>) =
230-
if source.IsCompletedSuccessfully then
231-
source.GetAwaiter().GetResult() |> ignore
232-
Unchecked.defaultof<_>
229+
let ignore (source: ValueTask) : ValueTask<unit> =
230+
if source.IsCompletedSuccessfully then Unchecked.defaultof<_>
233231
else
234-
new ValueTask (source.AsTask ())
232+
let tcs = TaskCompletionSource<unit> ()
233+
if source.IsFaulted then tcs.SetException (Unchecked.nonNull (source.AsTask().Exception)).InnerExceptions
234+
elif source.IsCanceled then tcs.SetCanceled ()
235+
else
236+
let k (t: ValueTask) : unit =
237+
if t.IsCanceled then tcs.SetCanceled ()
238+
elif t.IsFaulted then tcs.SetException (Unchecked.nonNull (source.AsTask().Exception)).InnerExceptions
239+
else tcs.SetResult ()
240+
if source.IsCompleted then k source
241+
else source.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted (fun () -> k source)
242+
tcs.Task |> ValueTask<unit>
235243

236244

237245
/// Raises an exception in the ValueTask

0 commit comments

Comments
 (0)