Skip to content

Commit 411cc42

Browse files
committed
worker: move terminate callback to end-of-life
Passing a callback to worker.terminate() has been deprecated for about six years now. It's time to remove it. PR-URL: nodejs#58528 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent d33f4b5 commit 411cc42

File tree

4 files changed

+11
-26
lines changed

4 files changed

+11
-26
lines changed

doc/api/deprecations.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2770,12 +2770,15 @@ legacy parser.
27702770

27712771
<!-- YAML
27722772
changes:
2773+
- version: REPLACEME
2774+
pr-url: https://github.com/nodejs/node/pull/58528
2775+
description: End-of-Life.
27732776
- version: v12.5.0
27742777
pr-url: https://github.com/nodejs/node/pull/28021
27752778
description: Runtime deprecation.
27762779
-->
27772780

2778-
Type: Runtime
2781+
Type: End-of-Life
27792782

27802783
Passing a callback to [`worker.terminate()`][] is deprecated. Use the returned
27812784
`Promise` instead, or a listener to the worker's `'exit'` event.

lib/internal/worker.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
ObjectEntries,
1212
Promise,
1313
PromiseResolve,
14+
PromiseWithResolvers,
1415
ReflectApply,
1516
RegExpPrototypeExec,
1617
SafeArrayIterator,
@@ -381,30 +382,21 @@ class Worker extends EventEmitter {
381382
ReflectApply(this[kPublicPort].postMessage, this[kPublicPort], args);
382383
}
383384

384-
terminate(callback) {
385+
terminate() {
385386
debug(`[${threadId}] terminates Worker with ID ${this.threadId}`);
386387

387388
this.ref();
388389

389-
if (typeof callback === 'function') {
390-
process.emitWarning(
391-
'Passing a callback to worker.terminate() is deprecated. ' +
392-
'It returns a Promise instead.',
393-
'DeprecationWarning', 'DEP0132');
394-
if (this[kHandle] === null) return PromiseResolve();
395-
this.once('exit', (exitCode) => callback(null, exitCode));
396-
}
397-
398390
if (this[kHandle] === null) return PromiseResolve();
399391

400392
this[kHandle].stopThread();
401393

402394
// Do not use events.once() here, because the 'exit' event will always be
403395
// emitted regardless of any errors, and the point is to only resolve
404396
// once the thread has actually stopped.
405-
return new Promise((resolve) => {
406-
this.once('exit', resolve);
407-
});
397+
const { promise, resolve } = PromiseWithResolvers();
398+
this.once('exit', resolve);
399+
return promise;
408400
}
409401

410402
async [SymbolAsyncDispose]() {

test/parallel/test-worker-nexttick-terminate.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ process.nextTick(() => {
1212
});
1313
`, { eval: true });
1414

15-
// Test deprecation of .terminate() with callback.
16-
common.expectWarning(
17-
'DeprecationWarning',
18-
'Passing a callback to worker.terminate() is deprecated. ' +
19-
'It returns a Promise instead.', 'DEP0132');
20-
2115
w.on('message', common.mustCall(() => {
22-
setTimeout(() => {
23-
w.terminate(common.mustCall()).then(common.mustCall());
24-
}, 1);
16+
setTimeout(() => w.terminate().then(common.mustCall()), 1);
2517
}));

test/parallel/test-worker-terminate-null-handler.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ process.once('beforeExit', common.mustCall(() => worker.ref()));
1515

1616
worker.on('exit', common.mustCall(() => {
1717
worker.terminate().then((res) => assert.strictEqual(res, undefined));
18-
worker.terminate(() => null).then(
19-
(res) => assert.strictEqual(res, undefined)
20-
);
18+
2119
}));
2220

2321
worker.unref();

0 commit comments

Comments
 (0)