1. /**
  2. * Paginate runtime environment results with fixed retry intervals.
  3. *
  4. * @param {Array} results - The array of results to paginate.
  5. * @param {number} pageSize - The number of results per page.
  6. * @param {number} retryInterval - The retry interval in milliseconds.
  7. * @param {function} processResult - A function to process each result.
  8. * @returns {Array<Array>} - An array of pages, where each page is an array of results.
  9. */
  10. function paginateRuntimeResults(results, pageSize, retryInterval, processResult) {
  11. if (!Array.isArray(results)) {
  12. console.error("Results must be an array.");
  13. return [];
  14. }
  15. if (typeof pageSize !== 'number' || pageSize <= 0) {
  16. console.error("Page size must be a positive number.");
  17. return [];
  18. }
  19. if (typeof retryInterval !== 'number' || retryInterval <= 0) {
  20. console.error("Retry interval must be a positive number.");
  21. return [];
  22. }
  23. if (typeof processResult !== 'function') {
  24. console.error("Process result must be a function.");
  25. return [];
  26. }
  27. const pages = [];
  28. for (let i = 0; i < results.length; i += pageSize) {
  29. const page = results.slice(i, i + pageSize); // Extract the current page
  30. pages.push(page);
  31. processResult(page); // Process the page
  32. //Simulate a retry with the specified interval
  33. setTimeout(() => {
  34. processResult(page);
  35. }, retryInterval);
  36. }
  37. return pages;
  38. }

Add your comment