我正在尝试创建一个工作池,其中一个函数获取网站上的某些数据。但我似乎无法从获取中得到任何东西。
还有另一种获取方式吗?还是我做错了什么?
这是我的代码:
运行脚本.js
const workerpool = require('workerpool');
const pool = workerpool.pool(__dirname + '/myWorker.js');
pool.proxy()
.then(function (worker) {
worker.asyncFetch().then(r => console.log(r));
})
.catch(function (err) {
console.error(err);
})
.then(function () {
pool.terminate(); // terminate all workers when done
});
myWorker.js
const workerpool = require('workerpool');
function asyncFetch () {
const fetch = require('node-fetch');
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(data => {
console.log('the data');
console.log(data)
});
return fetch('https://jsonplaceholder.typicode.com/todos/1');
}
// an async function returning a promise
function asyncMultiply (a, b) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve(a * b);
}, 1000);
});
}
// create a worker and register public functions
workerpool.worker({
asyncFetch: asyncFetch,
asyncMultiply: asyncMultiply
});