2 * Process multiple requests involving API calls safely to avoid getting jailed
4 export const processApiRequestsSafe = <T>(
5 promisesGenerators: (() => Promise<T>)[],
6 maxConcurrentPerInterval = 10,
7 intervalInMilliseconds = 1000
9 const queue = [...promisesGenerators];
10 let results: Promise<T>[] = [];
12 return new Promise((resolve) => {
14 const callbacks = queue.splice(0, maxConcurrentPerInterval);
15 const promises = callbacks.map((cb) => cb());
16 results = results.concat(promises);
18 setTimeout(run, intervalInMilliseconds);
20 resolve(Promise.all(results));