2
0

promise.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /**
  2. * Divide arrays into batches, and apply a given function to each batch by using Promise.all
  3. * Use when memory consumption can be too large by using simple Promise.all
  4. * @param items array to process
  5. * @param limit batch size
  6. * @param fn function to apply on each item
  7. * @param throwIfRejected whether or not to throw Error when there is a rejected Promise
  8. * @returns result of fn applied to each item
  9. */
  10. export const batchProcessPromiseAll = async<I, O>(
  11. items: Array<I>,
  12. limit: number,
  13. fn: (item: I, index?: number, array?: Array<I>) => Promise<O>,
  14. throwIfRejected = true,
  15. ): Promise<O[]> => {
  16. const results: O[] = [];
  17. for (let start = 0; start < items.length; start += limit) {
  18. const end = Math.min(start + limit, items.length);
  19. // eslint-disable-next-line no-await-in-loop
  20. const slicedResults = await Promise.allSettled(items.slice(start, end).map(fn));
  21. slicedResults.forEach((result) => {
  22. if (result.status === 'fulfilled') {
  23. results.push(result.value);
  24. }
  25. else if (throwIfRejected && result.reason instanceof Error) {
  26. throw result.reason;
  27. }
  28. });
  29. }
  30. return results;
  31. };