123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import batchExecute from '../libs_drpy/batchExecute.js';
- // 示例任务
- var results = [];
- var task = function (obj) {
- return new Promise((resolve) => {
- setTimeout(() => resolve(obj.url), 1000); // 模拟任务执行
- });
- };
- // 示例任务列表
- let tasks = [
- {
- func: task,
- param: {
- url: 'https://gitee.com/qiusunshine233/hikerView/raw/master/module/aes2.js',
- path: 'hiker://files/cache/t1.txt',
- },
- id: 'task1',
- },
- {
- func: task,
- param: {
- url: 'https://gitee.com/qiusunshine233/hikerView/raw/master/module/aes2.js',
- path: 'hiker://files/cache/t2.txt',
- },
- id: 'task2',
- },
- {
- func: task,
- param: {
- url: 'https://gitee.com/qiusunshine233/hikerView/raw/master/module/aes2.js',
- path: 'hiker://files/cache/t3.txt',
- },
- id: 'task3',
- },
- {
- func: task,
- param: {
- url: 'https://gitee.com/qiusunshine233/hikerView/raw/master/module/aes2.js',
- path: 'hiker://files/cache/t4.txt',
- },
- id: 'task4',
- },
- {
- func: task,
- param: {
- url: 'https://gitee.com/qiusunshine233/hikerView/raw/master/module/aes2.js',
- path: 'hiker://files/cache/t5.txt',
- },
- id: 'task5',
- },
- ];
- // 示例 listener
- var count = tasks.length;
- var success = 3;
- count = success;
- batchExecute(
- tasks,
- {
- func: function (obj, id, error, taskResult) {
- if (error) {
- console.log(`任务 ${id} 出错:`, error);
- return;
- }
- obj.results.push(taskResult);
- console.log(`任务 ${id} 完成,结果:`, taskResult);
- count--;
- if (count === 1) {
- console.log('我主动中断了');
- return 'break';
- } else if (count > 0) {
- console.log(`下载中,剩余任务:${count}`);
- } else {
- console.log('结束了');
- }
- },
- param: {
- hi: 'ccc',
- results: results,
- },
- },
- success
- ).then(() => {
- console.log(
- `任务数:${tasks.length},要求结果数:${success},返回结果数:${results.length}`
- );
- console.log('结果:', JSON.stringify(results));
- });
|