-
Notifications
You must be signed in to change notification settings - Fork 0
/
working.js
51 lines (47 loc) · 1018 Bytes
/
working.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
importScripts("https://unpkg.com/axios/dist/axios.min.js");
importScripts("https://unpkg.com/comlink/dist/umd/comlink.js");
const primeCheck = (x) => {
let t1 = Date.now();
let ans = [];
x.forEach(number => {
let res = null;
for (let i = 2; i < number; i++) {
if (number % i === 0) {
res = i;
break;
}
}
if (res === null) {
ans.push(number);
}
});
console.log("@@Inside worker:-", Date.now() - t1);
return ans;
}
const dataProcessing = (path, callback) => {
axios.get(path)
.then(res => {
console.log("received data file : ", res.data);
// Process data set here
let {
data
} = res;
data = data.split("\n");
data = data.slice(1);
let avgAge = 0;
data.forEach(tuple => {
let age = parseInt(tuple.split(",")[0]);
avgAge += age;
});
avgAge = avgAge / (data.length !== 0 ? data.length : 1);
callback(avgAge);
})
.catch(err => {
throw err;
});
};
const allFunctions = {
primeCheck,
dataProcessing
}
Comlink.expose(allFunctions);