Function createArray

  • Creates an array of size size and calls the given callback for each entry in the array and fills the array with the results. The returned array is typed to exactly fit the given size.

    This function automatically awaits async callbacks.

    Type Parameters

    • Size extends number
    • T

    Parameters

    • size: Size
    • callback: ((index: number) => T)
        • (index): T
        • Parameters

          • index: number

          Returns T

    Returns Extract<T, Promise<any>> extends never
        ? Tuple<T, Size>
        : Promise<Tuple<Awaited<T>, Size>>

    A new array filled with the results of the given callback typed as a Tuple, automatically wrapping the return type in a Promise if the callback returns one.

    import {createArray} from '@augment-vir/common';

    const result = createArray(5, (index) => {
    return `hi ${index}`;
    });
    // result is `['hi 0', 'hi 1', 'hi 2', 'hi 3', 'hi 4']`

    const asyncResult = await createArray(5, async (index) => {
    return Promise.resolve(`hi ${index}`);
    });
    // result is `['hi 0', 'hi 1', 'hi 2', 'hi 3', 'hi 4']`