【发布时间】:2021-07-07 23:39:23
【问题描述】:
预期行为:
.catch 块将错误打印到控制台后,代码将继续运行
行为:
代码退出程序。
尝试的方法:
我尝试使用返回;声明:什么也没做。我也尝试过使用 continue;声明,出错了。我还尝试将整个东西包装在一个 trycatch 中,这导致了相同的行为。
代码:
function readProxies() {
const rl = readline.createInterface({
input: fs.createReadStream(debug.proxy_file),
output: process.stdout,
terminal: false
});
rl.on('line', (line) => {
var ipPattern = new RegExp(/(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}/g);
var justIp = ipPattern.exec(line);
var oneProxy = ip2proxy.getAll(justIp[0])
if (oneProxy.Is_Proxy == 0) {
console.log('worked')
fetch('https://returning-json.workers-sites-examples.workers.dev/', {
agent: new ProxyAgent(debug.protocol + '://' + line, {
tunnel: true, // If true, will tunnel all HTTPS using CONNECT method
timeout: 2000, // Time in milli-seconds, to maximum wait for proxy connection to establish
})
})
.then(res => res.text())
.then(body => console.log(body))
.catch(err => {
console.log(err)
})
} else {
console.log('not worked')
}
});
rl.on('close', () => {
console.log('closed')
});
}
【问题讨论】:
-
你确定,这个错误是由
fetch引起的,因为这是目前唯一被catch保护的东西。如果异常发生在其他地方,没有什么可以捕捉到它... -
您能否详细说明您所看到的和您的期望?您是否只看到记录了一个错误然后“关闭”,好像只有一个
line事件正在处理? -
您在控制台上看到“关闭”消息了吗?因为这意味着 readline 的输入流已关闭 (nodejs.org/api/readline.html#readline_event_close) 但如果应用程序因异常而退出,则不会发生这种情况...
标签: javascript node.js promise readline