diff --git a/src/fable-library-beam/fable_async.erl b/src/fable-library-beam/fable_async.erl index df0b06307..056c803e7 100644 --- a/src/fable-library-beam/fable_async.erl +++ b/src/fable-library-beam/fable_async.erl @@ -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 @@ -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(). @@ -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) -> diff --git a/tests/Beam/TaskTests.fs b/tests/Beam/TaskTests.fs index 1d30e2470..1050c56a8 100644 --- a/tests/Beam/TaskTests.fs +++ b/tests/Beam/TaskTests.fs @@ -74,3 +74,17 @@ let ``test task sequential composition works`` () = return a + b } tsk.Result |> equal 3 + +[] +let ``test Async.AwaitTask works`` () = + let t = task { return 1 } + t |> Async.AwaitTask |> Async.RunSynchronously |> equal 1 + +[] +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