allKeyed

import { allKeyed } from "jsr:@ende/async";

function

function allKeyed<T extends Record<PropertyKey, unknown>>(record: PromiseRecord<T>): Promise<T>

Resolves all values in a record of promises in parallel, returning a promise that resolves to a record with the same keys and resolved values.

This is similar to Promise.all, but for records instead of arrays, allowing you to use named keys instead of positional indices.

If any promise rejects, the returned promise immediately rejects with the first rejection reason. The result object has a null prototype, matching the TC39 specification.

This function implements the behavior proposed in the TC39 Await Dictionary proposal (Promise.allKeyed).

record: PromiseRecord<T>

A record where values are promise-like (thenables) or plain values.

Returns

A promise that resolves to a record with the same keys and resolved values. The result has a null prototype.

Example: Basic usage

import { allKeyed } from "@ende/async/all-keyed";
import { assertEquals } from "@ende/assert";

const result = await allKeyed({
  foo: Promise.resolve(1),
  bar: Promise.resolve("hello"),
});

assertEquals(result, { foo: 1, bar: "hello" });

Example: Parallel HTTP requests

import { allKeyed } from "@ende/async/all-keyed";

const { user, posts } = await allKeyed({
  user: fetch("/api/user").then((r) => r.json()),
  posts: fetch("/api/posts").then((r) => r.json()),
});

Example: Mixed promises and plain values

import { allKeyed } from "@ende/async/all-keyed";
import { assertEquals } from "@ende/assert";

const result = await allKeyed({
  promised: Promise.resolve(42),
  plain: "static",
});

assertEquals(result, { promised: 42, plain: "static" });

Source