some code example outdated in the doc for worker_threads #21714
Closed
Description
- Version: 10.5.0
- Platform: Darwin
- Subsystem:
I found in the docs for the Worker following code example is outdated
const assert = require('assert');
const {
Worker, MessageChannel, MessagePort, isMainThread
} = require('worker_threads');
if (isMainThread) {
const worker = new Worker(__filename);
const subChannel = new MessageChannel();
worker.postMessage({ hereIsYourPort: subChannel.port1 }, [subChannel.port1]);
subChannel.port2.on('message', (value) => {
console.log('received:', value);
});
} else {
require('worker_threads').once('workerMessage', (value) => {
assert(value.hereIsYourPort instanceof MessagePort);
value.hereIsYourPort.postMessage('the worker is sending this');
value.hereIsYourPort.close();
});
}
should use parentPort.on("message")
replaced
Metadata
Assignees
Labels
No labels
Activity