Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions doc/api/worker_threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export default function parseJSAsync(script) {
workerData: script,
});
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code) => {
worker.once('error', reject);
worker.once('exit', (code) => {
if (code !== 0)
reject(new Error(`Worker stopped with exit code ${code}`));
});
Expand All @@ -73,8 +73,8 @@ if (isMainThread) {
workerData: script,
});
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code) => {
worker.once('error', reject);
worker.once('exit', (code) => {
if (code !== 0)
reject(new Error(`Worker stopped with exit code ${code}`));
});
Expand Down Expand Up @@ -670,17 +670,18 @@ share read and write access to the same set of environment variables.
import process from 'node:process';
import { Worker, SHARE_ENV } from 'node:worker_threads';
new Worker('process.env.SET_IN_WORKER = "foo"', { eval: true, env: SHARE_ENV })
.on('exit', () => {
.once('exit', () => {
console.log(process.env.SET_IN_WORKER); // Prints 'foo'.
});
```

```cjs
'use strict';

const process = require('node:process');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CJS snippets usually use the global process

Suggested change
const process = require('node:process');

const { Worker, SHARE_ENV } = require('node:worker_threads');
new Worker('process.env.SET_IN_WORKER = "foo"', { eval: true, env: SHARE_ENV })
.on('exit', () => {
.once('exit', () => {
console.log(process.env.SET_IN_WORKER); // Prints 'foo'.
});
```
Expand Down Expand Up @@ -950,7 +951,7 @@ const { port1, port2 } = new MessageChannel();
// foobar
// closed!
port2.on('message', (message) => console.log(message));
port2.on('close', () => console.log('closed!'));
port2.once('close', () => console.log('closed!'));

port1.postMessage('foobar');
port1.close();
Expand All @@ -966,7 +967,7 @@ const { port1, port2 } = new MessageChannel();
// foobar
// closed!
port2.on('message', (message) => console.log(message));
port2.on('close', () => console.log('closed!'));
port2.once('close', () => console.log('closed!'));

port1.postMessage('foobar');
port1.close();
Expand Down
Loading