Skip to content

Commit fedb0e7

Browse files
authored
Merge pull request #8 from insurello/delayed-fromtask
Lazify AsyncResult.fromTask
2 parents 505d591 + e019250 commit fedb0e7

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

src/Insurello.AsyncExtra.Tests/AsyncExtraTests.fs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ let taskTests =
5151
"fromUnitTask"
5252
[ testAsync "should convert from Task to AsyncResult" {
5353
let source = new CancellationTokenSource()
54-
let input: Task = Task.Delay(0, source.Token)
54+
let input: (unit -> Task) = fun () -> Task.Delay(0, source.Token)
5555

5656
let expectedValue = Ok()
5757

@@ -61,7 +61,7 @@ let taskTests =
6161
}
6262
testAsync "failing Task should result in Error" {
6363
let source = new CancellationTokenSource()
64-
let input: Task = Task.Delay(1000, source.Token)
64+
let input = fun () -> Task.Delay(1000, source.Token)
6565
let expectedValue = Error "A task was canceled."
6666

6767
source.Cancel()
@@ -74,7 +74,7 @@ let taskTests =
7474
"fromTask"
7575
[ testAsync "should convert from Task<string> to AsyncResult" {
7676
let input =
77-
Async.singleton "Hello" |> Async.StartAsTask
77+
fun () -> Async.singleton "Hello" |> Async.StartAsTask
7878

7979
let expectedValue = Ok "Hello"
8080

@@ -85,9 +85,10 @@ let taskTests =
8585
testAsync "fromTask failing Task should result in Error" {
8686

8787
let input =
88-
Async.singleton "Hello"
89-
|> Async.map (fun _ -> failwith "boom")
90-
|> Async.StartAsTask
88+
fun () ->
89+
Async.singleton "Hello"
90+
|> Async.map (fun _ -> failwith "boom")
91+
|> Async.StartAsTask
9192

9293
let expectedValue =
9394
Error "One or more errors occurred. (boom)"

src/Insurello.AsyncExtra/AsyncExtra.fs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,17 @@ module AsyncResult =
2121
| None -> Error err
2222
|> fromResult
2323

24-
let fromTask: System.Threading.Tasks.Task<'x> -> AsyncResult<'x, string> =
25-
fun task ->
26-
task
27-
|> Async.AwaitTask
24+
let fromTask: (unit -> System.Threading.Tasks.Task<'x>) -> AsyncResult<'x, string> =
25+
fun lazyTask ->
26+
async.Delay(lazyTask >> Async.AwaitTask)
2827
|> Async.Catch
2928
|> Async.map (function
3029
| Choice1Of2 response -> Ok response
3130
| Choice2Of2 exn -> Error exn.Message)
3231

33-
let fromUnitTask: System.Threading.Tasks.Task -> AsyncResult<unit, string> =
34-
fun task ->
35-
task
36-
|> Async.AwaitTask
32+
let fromUnitTask: (unit -> System.Threading.Tasks.Task) -> AsyncResult<unit, string> =
33+
fun lazyTask ->
34+
async.Delay(lazyTask >> Async.AwaitTask)
3735
|> Async.Catch
3836
|> Async.map (function
3937
| Choice1Of2 response -> Ok response

0 commit comments

Comments
 (0)