Skip to content

worker: update code examples for node:worker_threads module #58264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
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');
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
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.

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