diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index 26649b2eb429dd..8daeee4e20ad50 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -61,6 +61,35 @@ Worker threads inherit non-process-specific options by default. Refer to [`Worker constructor options`][] to know how to customize worker thread options, specifically `argv` and `execArgv` options. +## `worker.environmentData` + + +> Stability: 1 - Experimental + +Within a worker thread, `worker.environmentData` is a {Map} containing a clone +of data passed to the spawning thread's `worker.setEnvironmentData()` function. +The `worker.environmentData` is similar to `worker.workerData` except that +every new `Worker` receives it's own copy of `worker.environmentData` +automatically. + +```js +const { + Worker, + isMainThread, + setEnvironmentData, + environmentData +} = require('worker_threads'); + +if (isMainThread) { + setEnvironmentData('Hello', 'World!'); + const worker = new Worker(__filename); +} else { + console.log(environmentData.get('Hello')); // Prints 'World!'. +} +``` + ## `worker.isMainThread` - -An arbitrary JavaScript value that contains a clone of the data passed -to the spawning threads `worker.setPlatformData()` function. The -`worker.platformData` is similar to `worker.workerData` except that -every new `Worker` receives it's own copy of `platformData` automatically. - -```js -const { - Worker, - isMainThread, - setPlatformData, - platformData -} = require('worker_threads'); - -if (isMainThread) { - setPlatformData('Hello World!'); - const worker = new Worker(__filename); -} else { - console.log(platformData); // Prints 'Hello, world!'. -} -``` - ## `worker.receiveMessageOnPort(port)` +> Stability: 1 - Experimental + +* `key` {any} Any arbitrary, cloneable JavaScript value that can be used as a + {Map} key. * `value` {any} Any arbitrary, cloneable JavaScript value that will be cloned - and passed automatically to all new `Worker` instances. + and passed automatically to all new `Worker` instances. If `value` is passed + as `undefined`, any previously set value for the `key` will be deleted. -The `worker.setPlatformData()` API sets the value of the `worker.platformData` -in all new `Worker` instances spawned from the current context. +The `worker.setEnvironmentData()` API sets the content of the +`worker.environmentData` in all new `Worker` instances spawned from the current +context. -Calling `worker.setPlatformData()` will have no impact on the value of -`worker.platformData` on existing threads. +Calling `worker.setEnvironmentData()` will have no impact on the value of +`worker.environmentData` on existing threads. ## `worker.threadId`