worker.getEnvironmentData(key)


版本历史
版本变更
v17.5.0, v16.15.0

不再是实验的。

v15.12.0, v14.18.0

新增于: v15.12.0, v14.18.0

  • 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.

import {
 Worker,
 isMainThread,
 setEnvironmentData,
 getEnvironmentData,
} from 'node:worker_threads';
if (isMainThread) {
 setEnvironmentData('Hello', 'World!');
 const worker = new Worker(new URL(import.meta.url));
} else {
 console.log(getEnvironmentData('Hello')); // Prints 'World!'.
}'use strict';
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!'.
}

AltStyle によって変換されたページ (->オリジナル) /