From ffa511659cabc082afb6eb0ad2d0b0aa2f7ec8eb Mon Sep 17 00:00:00 2001 From: Dag Brattli Date: Sat, 18 Jul 2026 16:40:36 +0200 Subject: [PATCH] fix(beam): implement Async.AwaitTask MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `Async.AwaitTask` compiled to `fable_async:await_task/1`, which did not exist — the call built cleanly and failed at runtime with `undef`. On the Beam a Task and an Async share one representation (the `task` CE is compiled onto the async builder), so the conversion is the identity. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/fable-library-beam/fable_async.erl | 7 +++++++ tests/Beam/TaskTests.fs | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/fable-library-beam/fable_async.erl b/src/fable-library-beam/fable_async.erl index df0b063078..056c803e77 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 1d30e24701..1050c56a8e 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