diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index c0f762c54860a6..77b6a71b8b3ef9 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -2764,12 +2764,15 @@ legacy parser. -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. diff --git a/lib/internal/worker.js b/lib/internal/worker.js index 1df638ff77864c..d89d68f2227d0a 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -11,6 +11,7 @@ const { ObjectEntries, Promise, PromiseResolve, + PromiseWithResolvers, ReflectApply, RegExpPrototypeExec, SafeArrayIterator, @@ -381,20 +382,11 @@ 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(); @@ -402,9 +394,9 @@ class Worker extends EventEmitter { // 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]() { diff --git a/test/parallel/test-worker-nexttick-terminate.js b/test/parallel/test-worker-nexttick-terminate.js index 0e5d7e096c57ec..999b4e38161ba1 100644 --- a/test/parallel/test-worker-nexttick-terminate.js +++ b/test/parallel/test-worker-nexttick-terminate.js @@ -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); })); diff --git a/test/parallel/test-worker-terminate-null-handler.js b/test/parallel/test-worker-terminate-null-handler.js index 9db2e38b5c4920..e546e662655e06 100644 --- a/test/parallel/test-worker-terminate-null-handler.js +++ b/test/parallel/test-worker-terminate-null-handler.js @@ -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();