How to pass a class method in worker_theards node.js?

Hello!
if (isMainThread) {
 let modelUpDown = new Easy(
[500]
)

 const worker = new Worker(__filename, {})

 worker.on('message', (msg) => {
console.log(msg)
})

 worker.postMessage(modelUpDown) // give the class the worker
} else {
 parentPort.on('message', msg => {
 msg.action() //invoke a method on the class and ultimately undefined. How can I call this method?
})
}


Can't understand how to call a class method from the worker, writes undefined.
What to do? Please help
April 19th 20 at 12:40
1 answer
April 19th 20 at 12:42
Solution
Because functions are not serializable object
If anything, this is serialized: https://nodejs.org/docs/latest-v12.x/api/v8.html#v...
You can send a message, and the method the thread must call itself
Is it possible to serialize the entire class? understand that every method has to fix - Valerie_Mayert commented on April 19th 20 at 12:45
@Valerie_Mayert, again, You can't pass a function (methods are functions) in another thread. But You can exchange messages between threads. You can also create a separate channel for Your class and give it to one of the ports of the worker to communicate with a specific instance on a specific channel
https://nodejs.org/dist/latest-v12.x/docs/api/work... - ubaldo commented on April 19th 20 at 12:48

Find more questions by tags Node.js