Skip to content
Closed
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
5 changes: 4 additions & 1 deletion doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2764,12 +2764,15 @@ legacy parser.

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/58528
description: End-of-Life.
- version: v12.5.0
pr-url: https://github.com/nodejs/node/pull/28021
description: Runtime deprecation.
-->

Type: Runtime
Type: End-of-Life

Passing a callback to [`worker.terminate()`][] is deprecated. Use the returned
`Promise` instead, or a listener to the worker's `'exit'` event.
Expand Down
18 changes: 5 additions & 13 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
ObjectEntries,
Promise,
PromiseResolve,
PromiseWithResolvers,
ReflectApply,
RegExpPrototypeExec,
SafeArrayIterator,
Expand Down Expand Up @@ -381,30 +382,21 @@ class Worker extends EventEmitter {
ReflectApply(this[kPublicPort].postMessage, this[kPublicPort], args);
}

terminate(callback) {
terminate() {
debug(`[${threadId}] terminates Worker with ID ${this.threadId}`);

this.ref();

if (typeof callback === 'function') {
process.emitWarning(
'Passing a callback to worker.terminate() is deprecated. ' +
'It returns a Promise instead.',
'DeprecationWarning', 'DEP0132');
if (this[kHandle] === null) return PromiseResolve();
this.once('exit', (exitCode) => callback(null, exitCode));
}

if (this[kHandle] === null) return PromiseResolve();

this[kHandle].stopThread();

// Do not use events.once() here, because the 'exit' event will always be
// emitted regardless of any errors, and the point is to only resolve
// once the thread has actually stopped.
return new Promise((resolve) => {
this.once('exit', resolve);
});
const { promise, resolve } = PromiseWithResolvers();
this.once('exit', resolve);
return promise;
}

async [SymbolAsyncDispose]() {
Expand Down
10 changes: 1 addition & 9 deletions test/parallel/test-worker-nexttick-terminate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ process.nextTick(() => {
});
`, { eval: true });

// Test deprecation of .terminate() with callback.
common.expectWarning(
'DeprecationWarning',
'Passing a callback to worker.terminate() is deprecated. ' +
'It returns a Promise instead.', 'DEP0132');

w.on('message', common.mustCall(() => {
setTimeout(() => {
w.terminate(common.mustCall()).then(common.mustCall());
}, 1);
setTimeout(() => w.terminate().then(common.mustCall()), 1);
}));
4 changes: 1 addition & 3 deletions test/parallel/test-worker-terminate-null-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ process.once('beforeExit', common.mustCall(() => worker.ref()));

worker.on('exit', common.mustCall(() => {
worker.terminate().then((res) => assert.strictEqual(res, undefined));
worker.terminate(() => null).then(
(res) => assert.strictEqual(res, undefined)
);

}));

worker.unref();
Loading