if (value && value.data && value._id) {
// here you will receive messages
// console.log('This is the main thread ' + isMainThread)
- const res = this.runInAsyncScope(fn, null, value.data)
- this.parent.postMessage({ data: res, _id: value._id })
- this.lastTask = Date.now()
+ this._run(fn, value)
} else if (value.parent) {
// save the port to communicate with the main thread
// this will be received once
this.parent.postMessage({ kill: 1 })
}
}
+
+ _run (fn, value) {
+ try {
+ const res = this.runInAsyncScope(fn, null, value.data)
+ this.parent.postMessage({ data: res, _id: value._id })
+ this.lastTask = Date.now()
+ } catch (e) {
+ this.parent.postMessage({ error: e, _id: value._id })
+ this.lastTask = Date.now()
+ }
+ }
}
module.exports.ThreadWorker = ThreadWorker