worker.js
async function main() {
throw new Error('Bullocks!');
}
setTimeout(async () => {
await main();
}, 1000);
main.js
const { Worker } = require('worker_threads');
const worker = new Worker('./worker.js');
worker.on('error', err => {
console.log('error happened on the worker');
});
Why that error handler on the main.js is not called, instead it just prints the error.
(node:26000) UnhandledPromiseRejectionWarning: Error: Bullocks!
I've used this solution and works: throwing the error when unhandled rejection happens, but is there any "proper" solution for this?
async function main() {
throw new Error('Bullocks!');
}
setTimeout(async () => {
await main();
}, 1000);
process.on('unhandledRejection', err => {
throw err;
});
main()
outside, still not working.main()
itself is returning a rejected promise, then you can just.catch()
that and handle it. If the unhandled rejection is inside of main and not propagated out, then you have to find where that is in themain()
code and handle the error appropriately. There is no substitute for writing correct error handling unless you want to just let the process/thread abort because of it.