Skip to content

Commit 62bbe67

Browse files
committed
+ (Value)Task.ofResult
1 parent 620a89b commit 62bbe67

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
@@ -396,6 +396,16 @@ module Task =
396396
| Canceled -> tcs.SetCanceled ()
397397
source.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted (fun () -> k source)
398398
tcs.Task
399+
400+
/// Creates a Task from a Result value.
401+
/// If the Result is Ok, the Task will complete successfully with the value.
402+
/// If the Result is Error, the Task will complete unsuccessfully with the exception.
403+
/// <param name="source">The source Result.</param>
404+
/// <returns>The resulting Task.</returns>
405+
let ofResult (source: Result<'T, exn>) : Task<'T> =
406+
match source with
407+
| Ok x -> Task.FromResult x
408+
| Error exn -> Task.FromException<'T> exn
399409

400410
/// <summary>Creates a Task that's completed unsuccessfully with the specified exception.</summary>
401411
/// <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
@@ -364,6 +364,16 @@ module ValueTask =
364364
source.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted (fun () -> k source)
365365
ValueTask<'T> tcs.Task
366366

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

0 commit comments

Comments
 (0)