Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / shared / lib / api / helpers / safeApiRequests.ts
blob422a652610a15cab2d50f2166b2801091b0c572c
1 /**
2  * Process multiple requests involving API calls safely to avoid getting jailed
3  */
4 export const processApiRequestsSafe = <T>(
5     promisesGenerators: (() => Promise<T>)[],
6     maxConcurrentPerInterval = 10,
7     intervalInMilliseconds = 1000
8 ): Promise<T[]> => {
9     const queue = [...promisesGenerators];
10     let results: Promise<T>[] = [];
12     return new Promise((resolve) => {
13         const run = () => {
14             const callbacks = queue.splice(0, maxConcurrentPerInterval);
15             const promises = callbacks.map((cb) => cb());
16             results = results.concat(promises);
17             if (queue.length) {
18                 setTimeout(run, intervalInMilliseconds);
19             } else {
20                 resolve(Promise.all(results));
21             }
22         };
23         run();
24     });