Documentation
    Preparing search index...

    Function asyncRepeat

    • Executes an asynchronous function a specified number of times sequentially.

      Executes the function with indices from 0 to count-1 incrementally. Each execution is performed sequentially, and all results are collected into an array.

      Processing cost: The callback runs once for each of N indices, in sequence, with O(N) result storage.

      Resource ownership: The invocation awaits one callback at a time and retains only its partial output and current call; success transfers the output to the caller and rejection releases local state. Callback-owned external resources remain the callback owner’s responsibility.

      Type Parameters

      • T

        The type of the result from each execution

      Parameters

      • count: number

        The number of times to repeat (integer from 0 to 2^32 - 1)

      • closure: (index: number) => Promise<T>

        The asynchronous function to execute repeatedly

      Returns Promise<T[]>

      A Promise resolving to an array of results

      // Generate random data 5 times
      const randomData = await ArrayUtil.asyncRepeat(5, async (index) => {
      await new Promise((resolve) => setTimeout(resolve, 100)); // Wait 0.1 seconds
      return {
      id: index,
      value: Math.random(),
      timestamp: new Date().toISOString(),
      };
      });
      console.log("Generated data:", randomData);

      contracts/common.md#principled-implementation A count outside the integer range 0 through 2^32 - 1 is refused with a RangeError before any callback, because JavaScript arrays cannot represent a larger length; otherwise the closure is awaited for each index from 0 to count minus one and the results are collected in order, so the cost is linear in the count with no index array.

      contracts/common.md#clear-and-simple-design One validation and one loop; the validator is shared with repeat.

      contracts/common.md#prohibited-implementation-shortcuts The loop is the direct implementation of the documented behavior, and an invalid count is an error, not a coerced number.

      contracts/common.md#meaningful-documentation The comment states the index range, the sequential execution, and the result, and the count must be a non-negative integer as documented.