【问题标题】:Continue after promise rejection in .catch statement: Node.JS.catch 语句中的承诺拒绝后继续:Node.JS
【发布时间】: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


【解决方案1】:

编辑:derpirscher,谢谢!

打印到控制台的“关闭”让我对正在发生的事情有所了解。事实证明,一切都很顺利,我只需要在 catch 语句中添加一个return;,然后用更多代理填充 debug.proxy_file。他们中只有一个通过了第一次检查,所以只有一个被测试是有道理的。感谢您的帮助!

【讨论】:

    猜你喜欢
    • 2021-05-31
    • 2017-06-04
    • 2017-02-05
    • 2017-10-15
    • 1970-01-01
    • 2017-06-04
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多