diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index 37f19bc135d826..e9e5845d4ca19a 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -61,6 +61,38 @@ 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.getEnvironmentData(key)` + + +> Stability: 1 - Experimental + +* `key` {any} Any arbitrary, cloneable JavaScript value that can be used as a + {Map} key. +* Returns: {any} + +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. + +```js +const { + Worker, + isMainThread, + setEnvironmentData, + getEnvironmentData, +} = require('worker_threads'); + +if (isMainThread) { + setEnvironmentData('Hello', 'World!'); + const worker = new Worker(__filename); +} else { + console.log(getEnvironmentData('Hello')); // Prints 'World!'. +} +``` + ## `worker.isMainThread` + +> 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. If `value` is passed + as `undefined`, any previously set value for the `key` will be deleted. + +The `worker.setEnvironmentData()` API sets the content of +`worker.getEnvironmentData()` in the current thread and all new `Worker` +instances spawned from the current context. + ## `worker.threadId`