allSettledKeyed

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

function

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

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

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

Unlike allKeyed, this function never rejects due to promise rejections. Instead, each value in the result record is a PromiseSettledResult object indicating whether the corresponding promise was fulfilled or rejected. The result object has a null prototype, matching the TC39 specification.

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

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 PromiseSettledResult values. The result has a null prototype.

Example: Basic usage

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

const settled = await allSettledKeyed({
  success: Promise.resolve(1),
  failure: Promise.reject(new Error("oops")),
});

assertEquals(settled.success, { status: "fulfilled", value: 1 });
assertEquals(settled.failure.status, "rejected");

Example: Error handling

import { allSettledKeyed } from "@ende/async/all-keyed";
import { assertEquals, assertExists } from "@ende/assert";

const settled = await allSettledKeyed({
  a: Promise.resolve("ok"),
  b: Promise.reject(new Error("fail")),
  c: Promise.resolve("also ok"),
});

// Check individual results
if (settled.a.status === "fulfilled") {
  assertEquals(settled.a.value, "ok");
}
if (settled.b.status === "rejected") {
  assertExists(settled.b.reason);
}

Source