threads.js
threads.js copied to clipboard
feat: catch worker errors
Depends on https://github.com/avoidwork/tiny-worker/pull/43 Reject the promise if the worker was killed by the system or unexpectedly exited.
A nice feature of [email protected] was that you could listen for exit event of the spawned worker and reject the promise based on it.
await new Promise((resolve, reject) => thread
.run('./src/ChildProcessHelper')
.send({ data })
.on('exit', (code, signal) => {
if (code !== 0) {
reject(new Error(`child process exited with ${code} ${signal}`));
} else {
resolve([]);
}
})
.promise()
.then(resolve, reject));
With worker_threads this isn't possible at all, as they are running in the same process, so if something bad happens there, the main process is affected (e.g. segfault).
That's one more reason for enabling tiny-worker on latest node versions. https://github.com/andywer/threads.js/issues/229
The code just illustrates the idea.
Thanks for sharing that PR, @Ugzuzg.
Can you check why the new test case is failing?
@andywer, it fails because there is a dependency on a tiny-worker https://github.com/avoidwork/tiny-worker/pull/43 PR.