Skip to content

Commit c378640

Browse files
committed
+ (Value)Task.ofResult
1 parent ac47b33 commit c378640

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

src/FSharpPlus/Extensions/Task.fs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,16 @@ module Task =
397397
| Canceled -> tcs.SetCanceled ()
398398
source.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted (fun () -> k source)
399399
tcs.Task
400+
401+
/// Creates a Task from a Result value.
402+
/// If the Result is Ok, the Task will complete successfully with the value.
403+
/// If the Result is Error, the Task will complete unsuccessfully with the exception.
404+
/// <param name="source">The source Result.</param>
405+
/// <returns>The resulting Task.</returns>
406+
let ofResult (source: Result<'T, exn>) : Task<'T> =
407+
match source with
408+
| Ok x -> Task.FromResult x
409+
| Error exn -> Task.FromException<'T> exn
400410

401411
/// <summary>Creates a Task that's completed unsuccessfully with the specified exception.</summary>
402412
/// <param name="exn">The exception to be raised.</param>

src/FSharpPlus/Extensions/ValueTask.fs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,16 @@ module ValueTask =
365365
source.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted (fun () -> k source)
366366
ValueTask<'T> tcs.Task
367367

368+
/// Creates a Task from a Result value.
369+
/// If the Result is Ok, the Task will complete successfully with the value.
370+
/// If the Result is Error, the Task will complete unsuccessfully with the exception.
371+
/// <param name="source">The source Result.</param>
372+
/// <returns>The resulting Task.</returns>
373+
let ofResult (source: Result<'T, exn>) : ValueTask<'T> =
374+
match source with
375+
| Ok x -> ValueTask.FromResult x
376+
| Error exn -> ValueTask.FromException<'T> exn
377+
368378
/// <summary>Creates a ValueTask that's completed unsuccessfully with the specified exception.</summary>
369379
/// <param name="exn">The exception to be raised.</param>
370380
/// <returns>A ValueTask that is completed unsuccessfully with the specified exception.</returns>

0 commit comments

Comments
 (0)