Skip to content

some code example outdated in the doc for worker_threads #21714

@skyitachi

Description

@skyitachi
  • 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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions