Task Module
The Task module provides the built-in task API for concurrent execution.
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 -> 'afunction. - 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.writeLinemay 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}"