demo_be.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import batchExecute from '../libs_drpy/batchExecute.js';
  2. // 示例任务
  3. var results = [];
  4. var task = function (obj) {
  5. return new Promise((resolve) => {
  6. setTimeout(() => resolve(obj.url), 1000); // 模拟任务执行
  7. });
  8. };
  9. // 示例任务列表
  10. let tasks = [
  11. {
  12. func: task,
  13. param: {
  14. url: 'https://gitee.com/qiusunshine233/hikerView/raw/master/module/aes2.js',
  15. path: 'hiker://files/cache/t1.txt',
  16. },
  17. id: 'task1',
  18. },
  19. {
  20. func: task,
  21. param: {
  22. url: 'https://gitee.com/qiusunshine233/hikerView/raw/master/module/aes2.js',
  23. path: 'hiker://files/cache/t2.txt',
  24. },
  25. id: 'task2',
  26. },
  27. {
  28. func: task,
  29. param: {
  30. url: 'https://gitee.com/qiusunshine233/hikerView/raw/master/module/aes2.js',
  31. path: 'hiker://files/cache/t3.txt',
  32. },
  33. id: 'task3',
  34. },
  35. {
  36. func: task,
  37. param: {
  38. url: 'https://gitee.com/qiusunshine233/hikerView/raw/master/module/aes2.js',
  39. path: 'hiker://files/cache/t4.txt',
  40. },
  41. id: 'task4',
  42. },
  43. {
  44. func: task,
  45. param: {
  46. url: 'https://gitee.com/qiusunshine233/hikerView/raw/master/module/aes2.js',
  47. path: 'hiker://files/cache/t5.txt',
  48. },
  49. id: 'task5',
  50. },
  51. ];
  52. // 示例 listener
  53. var count = tasks.length;
  54. var success = 3;
  55. count = success;
  56. batchExecute(
  57. tasks,
  58. {
  59. func: function (obj, id, error, taskResult) {
  60. if (error) {
  61. console.log(`任务 ${id} 出错:`, error);
  62. return;
  63. }
  64. obj.results.push(taskResult);
  65. console.log(`任务 ${id} 完成,结果:`, taskResult);
  66. count--;
  67. if (count === 1) {
  68. console.log('我主动中断了');
  69. return 'break';
  70. } else if (count > 0) {
  71. console.log(`下载中,剩余任务:${count}`);
  72. } else {
  73. console.log('结束了');
  74. }
  75. },
  76. param: {
  77. hi: 'ccc',
  78. results: results,
  79. },
  80. },
  81. success
  82. ).then(() => {
  83. console.log(
  84. `任务数:${tasks.length},要求结果数:${success},返回结果数:${results.length}`
  85. );
  86. console.log('结果:', JSON.stringify(results));
  87. });