gpt4 book ai didi

javascript - 如何从 node-webworker-threads 向控制台发送错误?

转载 作者:搜寻专家 更新时间:2023-11-01 00:01:43 25 4
gpt4 key购买 nike

我正在开发一个 node.js 应用程序。我正在使用 webworker-threads包来分拆一些 CPU 阻塞任务。但是,我的线程代码中 Uncaught Error 不会发送到控制台。相反,线程只是默默地失败了。最简单的案例说明了这一点:

var Threads = require('webworker-threads');
this.testThread = new Threads.Worker( function() {
console.log("This is the test thread. The next line should throw an error.");
undefinedFunction();
} );

由于我的实际应用程序正在调用一个单独的 js 文件作为线程,所以我也没有看到任何语法错误报告。我应该捕获一些未记录的错误事件吗?我的环境可能有问题吗?

最佳答案

尝试绑定(bind)到 worker 的 onerror 事件:

var webpackWorker = new Worker(function() {             
this.onmessage = function(event) {
throw ('test');
};
});

webpackWorker.onerror = function (event) {
console.log(event);
}

webpackWorker.postMessage("dummy");

关于javascript - 如何从 node-webworker-threads 向控制台发送错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28177794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com