Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/fable-library-beam/fable_async.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ignore/1,
from_continuations/1,
start_as_task/1,
await_task/1,
cancellation_token/0,
create_cancellation_token/0, create_cancellation_token/1,
wrap_error/1
Expand Down Expand Up @@ -41,6 +42,7 @@
-spec ignore(async(term())) -> async(ok).
-spec from_continuations(fun()) -> async(term()).
-spec start_as_task(async(term())) -> term().
-spec await_task(async(term())) -> async(term()).
-spec cancellation_token() -> async(reference() | undefined).
-spec create_cancellation_token() -> reference().
-spec create_cancellation_token(term()) -> reference().
Expand Down Expand Up @@ -315,6 +317,11 @@ from_continuations(F) ->
start_as_task(Computation) ->
run_synchronously(Computation).

%% AwaitTask: identity. On the Beam a Task and an Async share one representation
%% (the `task` CE is compiled onto the async builder), so there is nothing to
%% convert — the task *is* the async computation.
await_task(Task) -> Task.

%% CancellationToken: returns async that extracts cancel_token from context
cancellation_token() ->
fun(Ctx) ->
Expand Down
14 changes: 14 additions & 0 deletions tests/Beam/TaskTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,17 @@ let ``test task sequential composition works`` () =
return a + b
}
tsk.Result |> equal 3

[<Fact>]
let ``test Async.AwaitTask works`` () =
let t = task { return 1 }
t |> Async.AwaitTask |> Async.RunSynchronously |> equal 1

[<Fact>]
let ``test Async.AwaitTask works inside async CE`` () =
let comp = async {
let! x = task { return 10 } |> Async.AwaitTask
let! y = task { return 20 } |> Async.AwaitTask
return x + y
}
comp |> Async.RunSynchronously |> equal 30
Loading