Under heavy development. This site describes the intended 1.0 language. Current features and performance vary. Development status
Sn50SnThe SnAPI programming language
0.2.0 Preview 1 · Frozen documentation

WhenAll

Joins owned task carriers and collects values in traversal order.

Sn is under heavy development. Reference extracted from this release’s bundled standard library. Public API examples are not an exhaustive compatibility test. Read the matching release guide for tested workflows and limitations. Read the status →

Module std.task

import std.task;

Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.

WhenAllAsync

pub async fn WhenAllAsync<T, I, A = DefaultAllocator>(mut Tasks: I, mut OutputAllocator: A)
    -> Task<Result<Vector<T, A>, AllocationError>>
    where T: Movable where I: TypedIterator<Task<T>>, Movable
    where A: Allocator, Defaultable, Movable

Joins owned task carriers and collects values in traversal order. T: Exact Movable completion value. I: Structural task cursor. A: Owned output allocator, defaulting to DefaultAllocator. Tasks: Owned traversal state; transfer a non-Copy cursor explicitly. OutputAllocator: Owned allocator used only for result storage. Return: Ordered owned values, or the first allocation error after all inputs finish. Existing Result completions remain ordinary elements, including their errors. Warning: Reference cursors require stable input backing until this task completes. Lazy task-producing Next calls retain their ordinary creation/pull costs. Allocation failure destroys collected and subsequent values, but still joins every remaining input. No executor thread blocks or implicit cancellation occurs.

Example

module example.ordered_when_all;
from std.task import Task, WhenAllAsync, YieldAsync;
from std.collections import Vector;
from std.prelude import Result;
async fn AnswerAsync(Value: i32) -> Task<Result<i32, i32>> {
    switch await YieldAsync() { case .Ok => {} case .Err => return .Err { Error = 1 }; }
    return .Ok { Value = Value };
}
async fn Main() -> Task<i32> {
    var Tasks = Vector<Task<Result<i32, i32>>>();
    if Tasks.TryPush(AnswerAsync(20)) is .Err { Error = _ } { return 1; }
    if Tasks.TryPush(AnswerAsync(22)) is .Err { Error = _ } { return 2; }
    var Results = {
        // Keep writable result storage through the explicit noncopy payload transfer.
        var ResultsCreation = await WhenAllAsync<Result<i32, i32>>(move(Tasks));
        switch ResultsCreation {
            case .Ok { Value = Values: mut Vector<Result<i32, i32>>& } => break move(Values);
            case .Err => return 3;
        }
    };
    if Results.Length != 2 { return 4; }
    switch Results.At(0) { case .Ok { Value = Value } => { if Value != 20 { return 5; } } case .Err => return 6; }
    switch Results.At(1) { case .Ok { Value = Value } => { if Value != 22 { return 7; } } case .Err => return 8; }
    return 0;
}

WhenAllAsync

pub fn WhenAllAsync<T, I, A = DefaultAllocator>(mut Tasks: I)
    -> Task<Result<Vector<T, A>, AllocationError>>
    where T: Movable where I: TypedIterator<Task<T>>, Movable
    where A: Allocator, Defaultable, Movable

Supplies the omitted arguments to WhenAllAsync. Note: Defaults: OutputAllocator = default(A). T: Same generic argument and constraints as the full operation. I: Same generic argument and constraints as the full operation. A: Same generic argument and constraints as the full operation. Tasks: Same input, ownership and lifetime contract as the full operation. Return: Exact full-operation result; no additional async frame is created.

WhenAllAsync

pub async fn WhenAllAsync<T, I, A = DefaultAllocator>(mut Tasks: I, mut OutputAllocator: A)
    -> Task<Result<Vector<T, A>, AllocationError>>
    where T: Movable where I: TypedIterator<mut Task<T>&>, Movable
    where A: Allocator, Defaultable, Movable

Joins stable writable task references and collects values in traversal order. T: Exact Movable completion value. I: Structural task cursor. A: Owned output allocator, defaulting to DefaultAllocator. Tasks: Owned traversal state; transfer a non-Copy cursor explicitly. OutputAllocator: Owned allocator used only for result storage. Return: Ordered owned values, or the first allocation error after all inputs finish. Existing Result completions remain ordinary elements, including their errors. Warning: Reference cursors require stable input backing until this task completes. Lazy task-producing Next calls retain their ordinary creation/pull costs. Allocation failure destroys collected and subsequent values, but still joins every remaining input. No executor thread blocks or implicit cancellation occurs.

WhenAllAsync

pub fn WhenAllAsync<T, I, A = DefaultAllocator>(mut Tasks: I)
    -> Task<Result<Vector<T, A>, AllocationError>>
    where T: Movable where I: TypedIterator<mut Task<T>&>, Movable
    where A: Allocator, Defaultable, Movable

Supplies the omitted arguments to WhenAllAsync. Note: Defaults: OutputAllocator = default(A). T: Same generic argument and constraints as the full operation. I: Same generic argument and constraints as the full operation. A: Same generic argument and constraints as the full operation. Tasks: Same input, ownership and lifetime contract as the full operation. Return: Exact full-operation result; no additional async frame is created.

WhenAllAsync

pub async fn WhenAllAsync<T, A = DefaultAllocator, O = DefaultAllocator>(mut Tasks: Vector<Task<T>, A>, mut OutputAllocator: O)
    -> Task<Result<Vector<T, O>, AllocationError>>
    where T: Movable where A: Allocator, Defaultable, Movable where O: Allocator, Defaultable, Movable

Keeps an owned task buffer stable while its writable iterator joins all elements. T: Exact Movable result. A: Owned input allocator. O: Output allocator. Tasks: Owned already-started task sequence, consumed after all joins. OutputAllocator: Owned result-storage allocator, defaulting to DefaultAllocator. Return: Results in original buffer order or drained storage failure.

WhenAllAsync

pub fn WhenAllAsync<T, A = DefaultAllocator, O = DefaultAllocator>(mut Tasks: Vector<Task<T>, A>)
    -> Task<Result<Vector<T, O>, AllocationError>>
    where T: Movable where A: Allocator, Defaultable, Movable where O: Allocator, Defaultable, Movable

Supplies the omitted arguments to WhenAllAsync. Note: Defaults: OutputAllocator = default(O). T: Same generic argument and constraints as the full operation. A: Same generic argument and constraints as the full operation. O: Same generic argument and constraints as the full operation. Tasks: Same input, ownership and lifetime contract as the full operation. Return: Exact full-operation result; no additional async frame is created.

Documentation snapshot: October 24, 2026 About versions ↗
Searching Development · language & standard library