W
W
WAYNEDEV2020-04-18 12:59:57
Node.js
WAYNEDEV, 2020-04-18 12:59:57

How to pass class method to node.js worker_teards?

Hello!

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

  const worker = new Worker(__filename, {})

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

  worker.postMessage(modelUpDown) // передаю класс воркеру
} else {
    parentPort.on('message', msg => {
      msg.action() //вызываю метод класса и в итоге он undefined. Как мне вызвать этот метод?
    })
}


I can’t figure out how to call a class method from a worker, writes undefined.
What do we have to do? help me please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2020-04-18
@WAYNEDEV

Because functions are not a serializable object
If anything, serialize this: https://nodejs.org/docs/latest-v12.x/api/v8.html#v...
You can send a message, and the stream method must call itself

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question