Promise.all with Tuples

Parallel Requests Keep Their Types

Promise.all([a, b, c]) runs promises in parallel. TypeScript types the result as a tuple, so each destructured variable gets its own type.

Example

Total time is about 100 ms (the slowest), not 190 ms, because the three calls run at the same time.

const delay = <T>(ms: number, value: T) => new Promise<T>((r) => setTimeout(() => r(value), ms));

const getUser = () => delay(100, { id: 1, name: "Riya" });
const getCart = () => delay(60, [{ sku: "SKU-1", qty: 2 }]);
const getWalletBalance = () => delay(30, 1520.5);

async function main() {
  const start = performance.now();
  const [user, cart, balance] = await Promise.all([getUser(), getCart(), getWalletBalance()]);
  // user: {id; name}, cart: {sku; qty}[], balance: number
  const ms = performance.now() - start;
  console.log(user.name, cart.length, balance.toFixed(2));
  console.log("parallel:", ms < 170 ? "yes" : "no");
}
main();

Output

Riya 1 1520.50
parallel: yes

Promise.allSettled

Example

When some calls may fail and you still want the others, use Promise.allSettled. Each result is a discriminated union on status.

const ok = <T>(v: T) => Promise.resolve(v);
const bad = (msg: string) => Promise.reject(new Error(msg));

async function main() {
  const results = await Promise.allSettled([ok("recommendations"), bad("reviews service down"), ok("offers")]);
  for (const r of results) {
    if (r.status === "fulfilled") console.log("OK  ", r.value);
    else console.log("FAIL", (r.reason as Error).message);
  }
}
main();

Output

OK   recommendations
FAIL reviews service down
OK   offers