worker.getEnvironmentData(key)


  • key <any> 任何可以用作 <Map> 键的任意、可克隆的 JavaScript 值。

    ¥key <any> Any arbitrary, cloneable JavaScript value that can be used as a <Map> key.

  • 返回:<any>

    ¥Returns: <any>

在工作线程中,worker.getEnvironmentData() 返回传给衍生线程的 worker.setEnvironmentData() 的数据的克隆。每个新的 Worker 都会自动接收到自己的环境数据的副本。

¥Within a worker thread, worker.getEnvironmentData() returns a clone of data passed to the spawning thread's worker.setEnvironmentData(). Every new Worker receives its own copy of the environment data automatically.

const {
  Worker,
  isMainThread,
  setEnvironmentData,
  getEnvironmentData,
} = require('node:worker_threads');

if (isMainThread) {
  setEnvironmentData('Hello', 'World!');
  const worker = new Worker(__filename);
} else {
  console.log(getEnvironmentData('Hello'));  // Prints 'World!'.
}