Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions doc/api/worker_threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -1846,6 +1846,14 @@ Calling `unref()` on a worker allows the thread to exit if this is the only
active handle in the event system. If the worker is already `unref()`ed calling
`unref()` again has no effect.

### `worker[Symbol.asyncDispose]()`

<!-- YAML
added: REPLACEME
-->

Alias for [`worker.terminate()`][].

## Notes

### Synchronous blocking of stdio
Expand Down
6 changes: 5 additions & 1 deletion lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const {
const { createMainThreadPort, destroyMainThreadPort } = require('internal/worker/messaging');
const { deserializeError } = require('internal/error_serdes');
const { fileURLToPath, isURL, pathToFileURL } = require('internal/url');
const { kEmptyObject } = require('internal/util');
const { kEmptyObject, SymbolAsyncDispose } = require('internal/util');
const { validateArray, validateString } = require('internal/validators');
const {
throwIfBuildingSnapshot,
Expand Down Expand Up @@ -406,6 +406,10 @@ class Worker extends EventEmitter {
});
}

async [SymbolAsyncDispose]() {
await this.terminate();
}

ref() {
if (this[kHandle] === null) return;

Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-worker-dispose.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as common from '../common/index.mjs';
import { Worker } from 'node:worker_threads';

{
// Verifies that the worker is async disposable
const worker = new Worker('for(;;) {}', { eval: true });
worker.on('exit', common.mustCall());
await worker[Symbol.asyncDispose]();
}
Loading