Javascript 对https使用try-catch,但不使用';我没有发现任何错误
我使用HTTPS从网站获取数据,我想做的是捕捉可能发生的任何错误, 但问题是它什么也抓不到,所以这是我的主要代码Javascript 对https使用try-catch,但不使用';我没有发现任何错误,javascript,node.js,asynchronous,async-await,try-catch,Javascript,Node.js,Asynchronous,Async Await,Try Catch,我使用HTTPS从网站获取数据,我想做的是捕捉可能发生的任何错误, 但问题是它什么也抓不到,所以这是我的主要代码 test = async() => { console.log("Hellow") now = new Date(); const https = require("https"); https.get("website",{ agent: proxyy }, (res) => {
test = async() => {
console.log("Hellow")
now = new Date();
const https = require("https");
https.get("website",{ agent: proxyy },
(res) => {
var body = "";
res.on("data", function (chunk) {
body += chunk;
});
res.on("end", function () {
var resp = JSON.parse(body);
data_wanted = resp.data
const desiredItem = data_wanted.find((item) =>
item.name.includes("data")
);
console.log(desiredItem)
});
}
);
};
我尝试了多种方法来捕捉这样的错误
async function run() {
try {
await test();
} catch (error) {
console.log(error)
}
也是这样
async function f() {
try{
run = await test()
}catch(e){
console.log("Hello world")
}
}
它尝试在函数内部使用try-catch,但也没有起作用,我猜想try-catch是在函数完成抓取之前执行的
编辑1:所以我的真正意图是做一个while循环,不断尝试,直到没有错误为止
const https = require('https');
https.get('https://encrypted.google.com/', (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
process.stdout.write(d);
});
}).on('error', (e) => {
console.error(e);
});
我很确定我的代码坏了,哈哈,我试过了,但没成功