Skip to main content
Version: Next

Task Module

The Task module provides the default task API for concurrent execution.

Task.spawn and Task.await are exposed through the default runtime extern registry, so embedding hosts may choose not to provide them.

Task type

Task values use the native postfix type form:

'a task

Task.spawn : (unit -> 'a) -> 'a task

Schedules a thunk for concurrent execution and returns an opaque task handle immediately.

let pending =
Task.spawn (fun () -> "done")

Notes:

  • The thunk is represented as a unit -> 'a function.
  • In script code, prefer fun () -> ....
  • The returned task can be awaited later.

Task.await : 'a task -> 'a

Waits for a task to complete and returns its result.

let pending = Task.spawn (fun () -> 40 + 2)
let answer = Task.await pending

Runtime behavior

  • Spawned thunks run concurrently.
  • Side effects such as Console.writeLine may interleave.
  • Task failures are fatal runtime errors.
  • A script cannot finish with unawaited tasks.

Example

let left = Task.spawn (fun () -> [1; 2; 3] |> List.map (fun n -> n * 2))
let right = [10; 20; 30] |> List.map (fun n -> n + 1)

let combined = (Task.await left) @ right
Console.writeLine $"{combined}"