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.

  // 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);
  • Parameters

    • count: number

      The number of times to repeat (non-negative integer)

    Returns <T>(closure: (index: number) => Promise<T>) => Promise<T[]>

    A function that takes an async closure and returns a Promise resolving to an array of results