-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
executable file
·41 lines (32 loc) · 782 Bytes
/
test.js
File metadata and controls
executable file
·41 lines (32 loc) · 782 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
39
40
41
#!/usr/bin/env node
class WorkQueue {
#jobs = [];
addJob(job) {
this.#jobs.push(job);
}
async *processJobs() {
for (const job of this.#jobs) {
const result = await job();
yield result;
}
}
}
// Simulated asynchronous job function
const simulatedJob = (id, delay = 1000) => {
return () =>
new Promise((resolve) =>
setTimeout(() => resolve(`Job ${id} completed`), delay)
);
};
// Test the WorkQueue class
(async () => {
const queue = new WorkQueue();
// Adding simulated jobs to the queue
queue.addJob(simulatedJob(1, 500));
queue.addJob(simulatedJob(2, 1000));
queue.addJob(simulatedJob(3, 1500));
// Execute and get results
for await (const result of queue.processJobs()) {
console.log(result);
}
})();