mediumJS ES6+#64

Promise.all for parallel requests

Prompt

Use Promise.all to fetch two users in parallel and return a promise of an array of their names. Simulate with Promise.resolve.

Solution

function fetchUser(id) {
  return Promise.resolve({ id, name: id === 1 ? 'Alice' : 'Bob' })
}

function getUsers() {
  return Promise.all([fetchUser(1), fetchUser(2)])
    .then(([user1, user2]) => [user1.name, user2.name])
}
Mentor's take

Promise.all is about when work starts, not how it's awaited. Both fetchUser calls execute immediately when the array literal is built — the requests are already in flight before Promise.all ever sees them. The combinator just aggregates settlement: it resolves with results in input order (not settlement order — index preservation is guaranteed even if the second request finishes first) and rejects fast with the first error, dropping the other results.

That fail-fast semantic is a choice, and the senior move is naming the family it belongs to: all for interdependent results (one failure invalidates the screen), allSettled when partial success is acceptable (independent batch operations — it never rejects, returning {status, value|reason} records), race for timeout patterns, any for redundant sources where the first fulfillment wins. Picking all when allSettled fits turns one failed request into a blank screen.

Red flag: const a = await fetchUser(1); const b = await fetchUser(2) — sequential awaits serialize independent requests into an N-round-trip waterfall. Same total code, double the latency. The parallel form starts both promises first and only then awaits.

Say it: "Promise.all preserves input order and fails fast on the first rejection — I choose the combinator by failure semantics: all when results are interdependent, allSettled when partial success is fine, race for timeouts."