Skip to content

joinAll ​

joinAll espera cada JoinHandle<T> de um array se estabelecer, resolvendo para seus resultados na mesma ordem em que os handles foram passados.

AssinaturajoinAll<T>(handles: JoinHandle<T>[]): Promise<T[]>

Resolve assim que todo handle já resolveu, não importa qual terminou primeiro de verdade, results[0] é sempre o resultado de handles[0], não o que se estabeleceu mais cedo. Rejeita com a primeira rejeição que observa, da mesma forma que Promise.all faz. Todo handle passado para joinAll é consumido do mesmo jeito que .join() consome um, reusar um handle já passado para joinAll, para race, ou para seu próprio .join(), é rejeitado em tempo de compilação (E3033).

tyrus
import { spawn, joinAll, sleepAsync } from "tyrus:task";

async function fetchShard(id: i32, delayMs: u64): Promise<i32> {
    // Stands in for a real per-shard request; `sleepAsync` simulates each
    // shard's own latency without needing real I/O for the example.
    await sleepAsync(delayMs);
    return id * 10;
}

async function main(): Promise<void> {
    const shardA = spawn(async (): Promise<i32> => await fetchShard(1, 20));
    const shardB = spawn(async (): Promise<i32> => await fetchShard(2, 10));
    // `joinAll` waits for every shard and keeps the results in the same
    // order the handles were given, regardless of which finishes first.
    const shards: i32[] = await joinAll([shardA, shardB]);
    print(shards[0] + shards[1]);
}