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.
Each await finishes before the next line runs, so fetchData("b") starts only after a is done.
JavaScript: Promise.all
Both calls start before anything is awaited. Promise.all resolves when the slower one does, with the results in order.
Both calls start before anything is awaited, so their waits overlap. Promise.all resolves when the slower one does.
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.
Both calls start before anything is awaited, so their waits overlap. WhenAll finishes when the slower one does.
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.
join! polls both futures on main's task. While a waits, b starts, so both calls overlap.
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.
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.
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