Compare

Wait for all: Promise.all, Task.WhenAll, join! and WaitGroup

Two network calls that don’t depend on each other should wait at the same time. Every language has a way to say “start all of these, then wait for all of them”, and on a timeline they all look alike: everything starts first, the waits overlap, and the total is about the slowest call.

The slow way: one by one

Each await finishes before the next line runs, so the second call starts only after the first ends. The waits add up.

JavaScriptPromise.all vs awaiting one by one

Each await finishes before the next line runs, so fetchData("b") starts only after a is done.

012345678910111213await fetchData("a")await fetchData("b")script: log a blogfetch a: a startsawait sleep(100)fetch a: a endsendsfetch b: b startsawait sleep(100)fetch b: b endsends
Play it step by step

JavaScript: Promise.all

Both calls start before anything is awaited. Promise.all resolves when the slower one does, with the results in order.

JavaScriptStart both, then await Promise.all

Both calls start before anything is awaited, so their waits overlap. Promise.all resolves when the slower one does.

012345678await Promise.allscript: log a blogfetch a: a startsawait sleep(100)fetch a: a endsendsfetch b: b startsawait sleep(100)fetch b: b endsends
Play it step by step

C#: Task.WhenAll

Calling an async method already starts it: each fetch runs to its first await before WhenAll is even called. WhenAll only waits.

C#Start both, then await Task.WhenAll

Both calls start before anything is awaited, so their waits overlap. WhenAll finishes when the slower one does.

012345678await Task.WhenAllMain: print a bprintfetch a: a startsawait Task.Delayfetch a: a endsendsfetch b: b startsawait Task.Delayfetch b: b endsends
Play it step by step

Rust: join!

tokio::join! polls both futures on main’s task. While one waits, the other runs. It’s concurrency on one thread, not parallelism.

RustRun both at once with join!

join! polls both futures on main's task. While a waits, b starts, so both calls overlap.

0123456789tokio::join!(…)main: print a bprintfetch a: a startssleep(100 ms).awaitfetch a: a endsendsfetch b: b startssleep(100 ms).awaitfetch b: b endsends
Play it step by step

Go: a WaitGroup and a channel

Each worker is its own goroutine. wg.Wait() waits for all of them, and the results come back on a channel with room for every one.

GoGetting results back from goroutines

Each worker sends its result on a channel with room for all three, so no send waits. main waits for the workers, closes the channel and adds up: sum: 14.

0123456789main: start 3 workersstartwg.Wait()main: close(results)closemain: range: sum the 3 resultssummain: print sum: 14printworker 1: square(1): sleepsworkworker 1: results <- 1: room, no waitsendworker 2: square(2): sleepsworkworker 2: results <- 4sendsquare(3): sleepsworker 3: results <- 9send
Play it step by step

Side by side

Wait for all Results
JavaScript await Promise.all(...) an array, in order
C# await Task.WhenAll(...) an array, in order
Rust tokio::join!(...) a tuple
Go wg.Wait() on a channel

Errors differ too: Promise.all rejects as soon as one call fails, while Task.WhenAll rethrows only after every task has finished.

Common questions

Why is awaiting calls one by one slow?

Each await finishes before the next line runs, so the second call starts only after the first ends, and the waits add up. Start every independent call first, then wait for all of them, and the waits overlap.

What happens when one of the calls fails?

It depends on the language. Promise.all rejects as soon as one promise rejects, while the others are still running; await Task.WhenAll rethrows only after every task has finished. In JavaScript, Promise.allSettled waits for all of them.

Does join! run the futures on several threads?

No. tokio::join! polls both futures on the calling task's thread, and they overlap while one waits. To use the runtime's worker threads, tokio::spawn them instead.

How do I get results back from goroutines?

Send them on a channel. Give it room for every result before wg.Wait(), or read while the workers send: waiting first on an unbuffered channel stops with all goroutines are asleep - deadlock!

Every timeline here is checked against a real program in CI. How we verify