-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
29 lines (24 loc) · 900 Bytes
/
worker.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
const {parentPort} = require('worker_threads')
const axios = require("axios");
parentPort.on('message', workerMessage => worker(workerMessage))
async function worker(data) {
if (data.data instanceof Object) {
const promiseArray = data.data.map(url => sendRequest(url))
await Promise.all(promiseArray);
await parentPort.postMessage(`Worker #${data.index} has finished`)
await parentPort.close()
}
}
function sendRequest(url) {
return new Promise((resolve, reject) => {
axios.get(url).then((response) => {
console.log(`[${response.status}] ${url}`)
return resolve(response);
}).catch((error) => {
console.error(`[${error.response.status ?? error.response.statusText}] ${url}`)
return reject(error)
})
})
}
// Cancel warning axios
process.on('unhandledRejection', error => {});