-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpauseAsyncTask.js
More file actions
38 lines (37 loc) · 881 Bytes
/
pauseAsyncTask.js
File metadata and controls
38 lines (37 loc) · 881 Bytes
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
export default pauseAsyncTask = (tasks) => {
let isRunning = false;
let result = [];
let i = 0;
return {
start() {
return new Promise(async (resolve, reject) => {
if (isRunning) {
console.log("Task is already running.");
return;
}
isRunning = true;
while (i < tasks.length) {
const task = tasks[i++];
try {
const res = await task();
result.push(res);
} catch (error) {
console.error("Error executing task:", error);
reject(error);
return;
}
if (!isRunning) {
console.log("Task paused.");
return;
}
}
isRunning = false;
resolve(result);
});
},
pause() {
isRunning = false;
console.log("Task paused.");
},
};
};