The type of the result from each execution
The number of times to repeat (non-negative integer)
The asynchronous function to execute repeatedly
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);
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.