Function allSettled

  • Typed version of Promise.allSettled, which defines two additional properties on the result: isFulfilled and isRejected.

    Type Parameters

    • T extends [] | readonly unknown[]

    Parameters

    • values: T

      An array of promises.

    Returns Promise<{
        -readonly [P in keyof T]: SettledResult<Awaited<T[P]>>
    }>

    A promise that resolves to an array of settled results.

    Example

    Basic usage

    async function getUser(name: string): Promise<{ id: string }> {...}
    const results = await allSettled([
    getUser('alice'),
    getUser('nonexistent'), // will be rejected
    ])
    // results[0].isFulfilled // true
    // results[1].isRejected // true

    Type narrowing

    results.forEach((result) => {
    if (result.isFulfilled) {
    result.value // type: { id: string }
    } else {
    result.reason // type: unknown (Error)
    }
    })

    Usage with partition

    import { isFulfilled, partition } from "uft";

    const [fulfilled, rejected] = partition(results, isFulfilled)
    fulfilled.forEach((result) => {
    result.value // type: { id: string }
    })
    rejected.forEach((result) => {
    result.reason // type: unknown (Error)
    })
  • Type Parameters

    • T

    Parameters

    • values: Iterable<T | PromiseLike<T>>

    Returns Promise<SettledResult<Awaited<T>>[]>

Generated using TypeDoc