Skip to content
Merged
Changes from 1 commit
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
Next Next commit
worker: update code examples for node:worker_threads module
  • Loading branch information
fisker committed May 10, 2025
commit 3d8d62c5cf3da866a0c9dee08663af3ee81a8f87
21 changes: 11 additions & 10 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');
Comment thread
fisker marked this conversation as resolved.
Outdated
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 Expand Up @@ -1384,7 +1385,7 @@ and what kind of JavaScript values can be successfully transported through
the thread barrier.

```mjs
import assert from 'node:assert';
import assert from 'node:assert/strict';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The rest of the changes here are ok but this one is generally unnecessary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

import {
Worker, MessageChannel, MessagePort, isMainThread, parentPort,
} from 'node:worker_threads';
Expand All @@ -1407,7 +1408,7 @@ if (isMainThread) {
```cjs
'use strict';

const assert = require('node:assert');
const assert = require('node:assert/strict');
const {
Worker, MessageChannel, MessagePort, isMainThread, parentPort,
} = require('node:worker_threads');
Expand Down
Loading