You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Transforms a `Task<'T>` into a `Task<Result<'T, exn>>`, catching any exceptions thrown by the task and wrapping them in the `Error` case.
6
+
7
+
Unlike [`ofTask`](ofTask.md), this function catches exceptions that escape from the task and maps them to `Error`. If the task completes successfully, the result is wrapped in `Ok`.
8
+
9
+
## Function Signature
10
+
11
+
```fsharp
12
+
Task<'T> -> Task<Result<'T, exn>>
13
+
```
14
+
15
+
## Examples
16
+
17
+
### Example 1
18
+
19
+
```fsharp
20
+
let result = TaskResult.ofCatchTask (task { return 42 })
21
+
// task { return Ok 42 }
22
+
```
23
+
24
+
### Example 2
25
+
26
+
```fsharp
27
+
let result = TaskResult.ofCatchTask (task { failwith "something went wrong" })
28
+
// task { return Error (System.Exception("something went wrong")) }
Copy file name to clipboardExpand all lines: gitbook/taskResult/ofTask.md
+7-3Lines changed: 7 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,9 @@
2
2
3
3
Namespace: `FsToolkit.ErrorHandling`
4
4
5
-
Transforms a `Task<'T>` into a `Task<Result<'T, exn>>`.
5
+
Transforms a `Task<'T>` into a `Task<Result<'T, exn>>` by wrapping the value in `Ok`.
6
+
7
+
> **Note:** This function does **not** catch exceptions thrown by the task. Any exceptions will propagate as-is. To catch exceptions and map them to the `Error` case, use [`ofCatchTask`](ofCatchTask.md).
6
8
7
9
## Function Signature
8
10
@@ -22,6 +24,8 @@ let result = TaskResult.ofTask (task { return 42 })
22
24
### Example 2
23
25
24
26
```fsharp
25
-
let result = TaskResult.ofTask (task { return System.Exception("error") })
0 commit comments