-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathdemo_be.js
92 lines (87 loc) · 2.35 KB
/
demo_be.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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));
});