问题描述
我正在尝试创建一个工作池,其中一个函数获取网站上的某些数据。但我似乎无法从 fetch 中得到任何东西。
还有其他获取方式吗?还是我做错了什么?
这是我的代码:
run-script.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
});
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)